Home > Back-end >  How to Display Map in Flutter Widget by Looping Them
How to Display Map in Flutter Widget by Looping Them

Time:04-03

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.

CodePudding user response:

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