Search code examples
flutterdartmultimap

MultiMap in Flutter


I would like to have a multimap or ListMultiMap in flutter, since I require multiple values with same key, when I use dart's quiver collection as per the dart document, MultiMap showing error, saying no such method. Can anyone help me please. imported quiver collection and then tried using Multimap class

import 'package:quiver/collection.dart';

https://pub.dev/documentation/quiver/latest/quiver.collection/Multimap-class.html

tried using Multimap as per above documentation, but showing error

Multimap<String, String> multimap = new MultiMap<String,String>();

Solution

  • You might have a typo. Note that you are saying new MultiMap with the M of map capitalized. (Note that the new keyword isn't needed and should be dropped.)

    The following works as expected:

    import 'package:quiver/collection.dart';
    
    void main() {
      var myMap = Multimap<String, String>();
      myMap.add('a', 'a1');
      myMap.add('a', 'a2');
      myMap.forEach((key, value) => print('[$key->$value]'));
    }
    

    and prints:

    [a->a1]
    [a->a2]