'How to Display Map in Flutter Widget by Looping Them
I understand how to use .toList(), but the data should be in List. How if I would like to literate map data into widget. Here is the example code
class BundlingLocation extends StatelessWidget {
final DataAd object;
BundlingLocation({required this.object});
final List location = ['province','regency','district','village'];
@override
Widget build(BuildContext context) {
final json = object.toJson();
Map AdLocation = {};
for(var u in location){
if(json[u]!=null && json[u]!=''){
AdLocation[u] = json[u];
}
}
//returning all location into row
return Row(
children: AdLocation.map((key, value) => Text(value)), ==> get an error
);
}
}
Anyone knows how to achieve this? I really appreciate any answers. Thank you.
Solution 1:[1]
You can show your map values using the MapEntry() instead of just passing Text() to .map().
Row(
children: AdLocation
.map((key, value) => MapEntry(key, Text(value)))
.values
.toList(),
),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chirag Bargoojar |
