Home > Software engineering >  How to read complex list in dart/flutter
How to read complex list in dart/flutter

Time:08-24

I have a list like this.

[{ a, 2 }, { b, 2 }, { c, 2 }, { d, 1 }, { e, 2 }, { f, 1 }, { g, 1 }, { h, 3 }]

I want toread this list. for example ı want to print a or print 2. How can ı do this

CodePudding user response:

This is essentially a list of maps or a list of lists. In both cases, you could simply iterate over the 'root' list and for the child, print the different values.

// list with some random maps and lists inside it
List<dynamic> myList = [{'a': 2}, ['b', 2], ...];

Now you can iterate it and, depending on the contents, print them:

for (var item in myList) {
  print(item);
  // depending on the type, print/process it
  if (item is List) {
    for (var nestedItem in item) {
      ... // print each nested item
    }
  } else if (item is Map) {
    print(item.keys);
    print(item.values);
    print(item.entries);
  }
}

Depending on your needs, you can tweak/combine the code above.

  • Related