自定义类集交叉点在DART中不起作用
以下是代码:
final Set<Item> _set1 = {
Item(id: 1, name: "carrot"),
Item(id: 2,name: "apple"),
Item(id: 3,name: "mango")
};
final Set<Item> _set2 = {
Item(id: 1, name: "carrot"),
};
print(_set1.intersection(_set2));
我的班级看起来像这样:
class Item{
int id;
String name;
Item({required this.id,required this.name});
}
必需的输出:
{Instance of 'Item'}
//carrot which is common
以上代码出现的输出:
{}
The Following is the code:
final Set<Item> _set1 = {
Item(id: 1, name: "carrot"),
Item(id: 2,name: "apple"),
Item(id: 3,name: "mango")
};
final Set<Item> _set2 = {
Item(id: 1, name: "carrot"),
};
print(_set1.intersection(_set2));
My Class looks like this:
class Item{
int id;
String name;
Item({required this.id,required this.name});
}
Required output:
{Instance of 'Item'}
//carrot which is common
Output that came out of above code:
{}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DART中的一个文字创建A
换句话说,您必须覆盖
==
和hashcode
在item
类中。A set literal in dart creates a LinkedHashSet, which has the following requirement:
In other words you must override
==
andhashCode
in yourItem
class.