我有两个班级
class Phone extends Observable
{
@observable String type = '';
@observable String provider = '';
@observable String num = '';
Map<String, Map<String, String> map = {};
Phone() {}
Phone.build({ this.type,
this.provider,
this.num });
}
我试图将字段值用作地图中的键,如此
Phone phone = new Phone();
phone.map[phone.type] = {'type':'cell', 'provider':'Verizon', 'num':'1234567'};
但它不起作用。如何让字段成为地图的关键值?
答案 0 :(得分:2)
只需删除引号
即可phone.map[phone.type] = {type:'cell', provider:'Verizon', num:'1234567'};
因为您在示例中使用了字符串,所以这可能不适用,但请注意,如果您使用自定义类型的实例作为Map键...
The keys of a `HashMap` must have consistent [Object.operator==]
and [Object.hashCode] implementations. This means that the `==` operator
must define a stable equivalence relation on the keys (reflexive,
anti-symmetric, transitive, and consistent over time), and that `hashCode`
must be the same for objects that are considered equal by `==`.