I have a set<list<map<string,dynamic>>> and ı must convert it to map<string,dynamic> to upload firebase. can anybody help? note: ı'm new to this site and I can make a mistake please warn me


Solution 1: R. drt

A map itself will store multiple key-value pair data, inside a list means you have a list with multiple maps, then again you have that inside a Set. you cant quite convert it to a single object, what you can do is iterate these collections and get what you want I suppose, you might want to run a loop on the set, inside loop the list and that will give you each map object.


Solution 2: Mabsten

Map is conceptually similar to Set, except that it allows you to use arbitrary keys (while in Set a numeric index, from zero to Set.length, is used as key) and allows the insertion of double elements (while Set does not). Then you can create a Map that uses as keys the same indices of the Set.

Example:

  Set<dynamic> set = {"a", "b"};
  Map<String, dynamic> map = {};
  for (var index = 0; index < set.length; index++) {
    map[index.toString()] = set.elementAt(index);
  }
  print(map); //print: {0: a, 1: b}

However, generally for serialization in Dart (such as in firebase database, or json decoding with dart:convert package, or dart-javascript interop) the following types are accepted/used, often called primitives (in the context of dart serialization):

  • String,
  • int (and in some context even double, for example is accepted by DatabaseReference.set() of firebase database)
  • bool,
  • Map with String keys
  • List

Map and List can in turn contain objects of any of the primitive classes, for example other Map, List, etc (and even the parametric type can actually be different from dynamic).

So in your case a conversion from Set to List would also be fine.

set.toList();

For the rest, your data structure is already made up of primitives, ie it is ok for serialization in firebase database or other systems. Then you can completely ignore the fact that your Map (or List, if you prefer) has parametric type List <Map<etc>> and not dynamic.