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.
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.
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.
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.
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
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With