Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read complex list in dart/flutter

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading