In my Flutter Widget I have a StreamBuilder
that checks for snapshot.hasError
and in this specific case it will return my ErrorRetryWidget()
.
builder: (context, AsyncSnapshot<MyObject> snapshot) {
...
if (snapshot.hasError) {
return ErrorRetryWidget();
}
}
The ErrorRetryWidget()
just shows an error message and a retry button. When you press this button, I replace the button text by a progress indicator. Therefore I needed to make this widget stateful, as its state holds a isRetrying
variable that I set to false in the initState
, and then to true once pressed.
When pressing the button, the ErrorRetryWidget
tells the parent through a VoidCallback
to re-trigger the stream logic. It works well but the issue is that if the error comes back, my StreamBuilder
will "return" the ErrorRetryWidget
once again.
The constructor is called a new time, but not initState
. How can I make it so the state resets every time the widget is re-created? Therefore, isRetrying
is already (or still) set to true.
The only quick solution I found was to implement this in my error widget:
@override
void didUpdateWidget(covariant oldWidget) {
super.didUpdateWidget(oldWidget);
setState(() {
retrying = false;
});
}
Not sure it's a good practice.