Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter listview with Map instead of List

Tags:

flutter

dart

I'm trying to display the contents of my HashMap values in a ListView.builder widget. Is there a way to do this? With a List I could simply use the index, but how would that work with a HashMap without making a List out of it? The keys of the map are strings and the values are maps with the data to display.

like image 737
Bram Vanbilsen Avatar asked Oct 20 '17 20:10

Bram Vanbilsen


People also ask

What is difference between map and list in flutter?

List, Set, Queue are iterable while Maps are not. Iterable collections can be changed i.e. their items can be modified, add, remove, can be accessed sequentially. The map doesn't extend iterable.

What is the difference between ListView and ListView builder and two properties of ListView builder in flutter?

ListView() constructor takes argument children where you can use map method of your list to create a list of widgets. These widgets are rendered immediately when the build method is called. ListView. builder() on the other hand, takes itemBuilder and itemCount parameters.

What can I use instead of ListView in flutter?

ListView is a very important widget in a flutter. It is used to create the list of children But when we want to create a list recursively without writing code again and again then ListView. builder is used instead of ListView.


2 Answers

Its a little late but You could also try this. Map values = snapshot.data;

return new ListView.builder(   itemCount: values.length,   itemBuilder: (BuildContext context, int index) {     String key = values.keys.elementAt(index);     return new Column(       children: <Widget>[         new ListTile(           title: new Text("$key"),           subtitle: new Text("${values[key]}"),         ),         new Divider(           height: 2.0,         ),       ],     );   }, ); 

for a more detailed example check this out https://kodestat.gitbook.io/flutter/39-flutter-listviewbuilder-using-dart-maps

like image 158
kenn Avatar answered Sep 17 '22 14:09

kenn


Just make a list from the keys and then get the value using the index to get the map key and use it to get the map value

var keys = myMap.keys.toList(); var val = myMap[keys[idx]]; 
like image 45
Günter Zöchbauer Avatar answered Sep 20 '22 14:09

Günter Zöchbauer