Home > database >  Flutter Filtering Future with list builder
Flutter Filtering Future with list builder

Time:02-04

I have a widget that is meant to return a list/listtile of amenities in a location, the data comes from a json file which i get when the page loads and displays a list of locations. the user then click on a location and gets a list of amenities in said location. can we do something like tmp = amenities.filter(el => el.locationid=locationid

class _Locations extends State<Locations>
    with SingleTickerProviderStateMixin {

late Future<Amenities> amenities;
@override
  void initState() {
    super.initState();
    amenities = AmenitiesDataApi.getAmenities();
  }

 Widget localAttractionsTab(locationid) {
    return Column(
      children: <Widget>[
        FutureBuilder(
          future: amenities,
          builder: (BuildContext context, AsyncSnapshot<Amenities> snapshot) {
            if (snapshot.hasData) {
              for (var amen in snapshot.data!.amenities) {
                if (amen.locationid == locationid) {
                  return ListTile(Text(snapshot.data!.amenities[0].amenityname),);
                }
              }
              throw ('error');
            }
          },
        ),
      ],
    );
  }

CodePudding user response:

That should be possible, but you need to re-arrange your widget a litte. The Column wants to see a <Widget>[] for its child parameter, so you can use filter and map on the list here:

Widget localAttractionsTab(locationid) {
  return FutureBuilder(
    future: amenities,
    builder: (BuildContext context, AsyncSnapshot<Amenities> snapshot) {
      if (snapshot.hasData) {
        return Column(
          children: snapshot.data!.amenities
            .where((el) => el.locationid == locationid)
            .map((el) => ListTile(Text(el.amenityname)))
            .toList()
        );
       }
       return Container();
    },
  );
}

CodePudding user response:

You can try this

 Widget localAttractionsTab(locationid) {
   return   FutureBuilder(
         future: amenities,
         builder: (BuildContext context, AsyncSnapshot<Amenities> 
       snapshot) {
        if (snapshot.hasData) {
         return Column(
          children:   snapshot.data!.amenities.where((amen) => amen.locationid == 
   locationid)
          .map((amen) =>  ListTile(Text(amen.amenityname),)
         ).toList());     
           
        }
        return CircularProgressIndicator();
      },
    
 );
}

CodePudding user response:

You can also update your code by moving the Column into the FutureBuilder and also do the filtration outside the for loop.

 Widget localAttractionsTab(locationid) {
return  FutureBuilder(
      future: amenities,
      builder: (BuildContext context, AsyncSnapshot<Amenities> snapshot) {
  return      Column(
  children: <Widget>[
    if (snapshot.hasData) {
          for (var amen in snapshot.data!.amenities.where((amen)=> amen.locationid == locationid)
                ListTile(Text(amen.amenityname))
    
        }
      ]);
   }),
    
  );
}
  • Related