flutter
flutter

Reputation: 6786

How to populate a multimap in Dart

I'm using a MultiMap from the quiver package. I'm trying to populate the map with 2 lists like I would with an ordinary map:

final keys = myMap.keys.toList();
final values = myMap.values.toList();

for (var i = 0; i < values.length; i++) {
      map[values[i]] = itemSpit[I];
    }

However the for loop doesn't compile: value

The operator '[]=' isn't defined for the type 'Multimap<dynamic, dynamic>'.

How can I add the lists to the multimap

Upvotes: 1

Views: 196

Answers (1)

jamesdlin
jamesdlin

Reputation: 90125

Dart has two versions of the square brackets operator; one for reading (operator []) and one for writing (operator []=). Multimap providers operator [] but does not provide operator []=. Presumably this is because it would be unclear to readers whether multimap[key] = value intends to add a new value or to replace the existing values.

Instead, Multimap provides add and addValues methods for adding values. (Replacing requires explicitly calling removeAll first.)

Upvotes: 1

Related Questions