Question

I have two classes

  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 });
  }

I have attempted to use the field values as the key in map like so

   Phone phone = new Phone();
   phone.map[phone.type] = {'type':'cell', 'provider':'Verizon', 'num':'1234567'};

but it does not work. How can I make the fields value the key for the map?

Was it helpful?

Solution

just remove the quotes

phone.map[phone.type] = {type:'cell', provider:'Verizon', num:'1234567'};

Because you are using strings in your example this may not apply but be aware that if you use instances of custom types as Map key...

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 `==`.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top