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
That should be possible, but you need to re-arrange your widget a litte.
The
Column
wants to see a<Widget>[]
for itschild
parameter, so you can usefilter
andmap
on the list here:You can try this
You can also update your code by moving the Column into the
FutureBuilder
and also do the filtration outside the for loop.