0
votes

So I have a FutureBuilder that calls different methods depending on what button is pressed.

child: FutureBuilder(
 future: buffetPressed
   ? API().getPackages(widget.restaurant.id)
   : API().getFoodItems(widget.restaurant.id),

Which returns a Listview.Builder

return ListView.builder(
 scrollDirection: Axis.vertical,
 shrinkWrap: true,
 itemCount: snapshot.data.length,

The problem is snapshot.data.length is different for each method as each api call returns different data. Everything else updates in the list (names, etc) but the length does not update causing RangeError (index): Invalid value: not in inclusive range. Which I understand is from the itemCount being wrong. Is there a way to update the itemcount when the button is pressed? I'm fairly new and I can't figure it out.

1
Shouldn't the snapshot.data is the output of API().getPackages or API().getFoodItems ? - John Joe
@JohnJoe snapshot.data changes based on a button press. By default it's packages but on press it changes to food. Then can be changed back by another press. - Yousef
Then it should not be a problem because snapshot.data are depends on the output of the two. - John Joe
The only problem is that itemCount: snapshot.data.length is not updating. Everything else is fine. - Yousef
It is updating, otherwise you wouldn't get Invalid value: not in inclusive range error. - John Joe

1 Answers

0
votes

I fixed it by switching from FutureBuilder to StreamBuilder.