Roohome Com
Roohome Com

Reputation: 97

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.

Upvotes: 1

Views: 1851

Answers (1)

Chirag Bargoojar
Chirag Bargoojar

Reputation: 1214

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(),
  ),

Upvotes: 3

Related Questions