i want to rebuild my FutureBuilder after if's builder leads to snapshot.hasError
I tried using Statefullbuilder to rebuild my FutureBuilder by making and changing random bool, is it a way to resolve problem? I really can't find out until problem comes out
bool boolin = false;
StatefulBuilder(
builder: (context, setState) => FutureBuilder(
initialData: '',
future: func,
builder: ((context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.connectionState ==
ConnectionState.done) {
if (snapshot.hasError) {
setState(() {
!booling;
});
setState.call;
return const Text('data1');
} else {
return const Text('data2');
}
} else {
return Text(snapshot.connectionState.toString());
}
})))
You can use a
StatefulWidgetand put thefuncvariable holding the future into a member of the state class. Initailize it first in theinitState, and then you can restart the future usingsetStateand re-assign the value of the future.But I really don't recommend to call it automatically on error. It could mean and endless loop if the error persists. Instead let the user invoke it with a button for example.
To re-run the future: