setState rebuilds the parent widget, causing a blink effect in FutureBuilders (because they rebuild?)

Viewed 704

In my parent widget i have 2 FutureBuilders

Image FutureBuilder

FutureBuilder(
              future: scannedUserBloc.getImageUrl(widget.userId),
              builder: (context, snapshot) {
                if(snapshot.connectionState == ConnectionState.done) {
                  return Container(
                      height: MediaQuery.of(context).size.height / 5,
                      width: MediaQuery.of(context).size.width / 5,
                      child: Image.network(snapshot.data)
                  );
                } else {
                  return Text("Image is loading");
                }
              }

List future builder

FutureBuilder(
    future: scannedUserBloc.getMembershipPrograms(widget.userId),
    builder: (context, snapshot) {
      if(snapshot.connectionState == ConnectionState.done) {
        var programs = snapshot.data as List<MembershipPrograms>;
        if(programs.length > 0) {
          hasPrograms = true;
          return programsRowWidget(context, programs);
        } else {
          return noProgramsColumnWidget(context);
        }
      }else {
        //loading indicator
        return Container(

        );
      }
});

When i want to click on an item in the ListView, i want it to change its border, so it looks like it is selected. To do that, i need to call setState, but when i call it, it causes FutureBuilders to blink. That happens because (i'm assuming) the widgets rebuild, which causes the FutureBuilders to make the async call to the backend, making the items disappear temporary until the items are fetched again.

Is there any workaround or fix for this? Is the approach i'm taking bad in general?

1 Answers

You can separate the ListView widget to different Stateful class. Then call setState from there. Then the FutureBuilder will not execute multiple times.

Related