skip to Main Content

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 clicks 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');
            }
          },
        ),
      ],
    );
  }

3

Answers


  1. 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();
        },
      );
    }
    
    Login or Signup to reply.
  2. 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();
          },
        
     );
    }
    
    Login or Signup to reply.
  3. 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))
        
            }
          ]);
       }),
        
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search