I have a TextField
within StreamBuilder
that listens to BehaviorSubject
stream. When snapshot has error, the errorText
displays it.
The problem is when TextField
is scrolled out of visible area and scrolled back in the StreamBuilder
rebuilds but error text is gone because this time snapshot.hasError
is false.
How to maintain the error?
You might want to store the error in a String
variable of you StatefulWidget
.
Once you are ready to clear the error (f.ex. a user presses a clear button) you simple set this variable to null.
String errorMsg;
StreamBuilder(
stream: myStream,
builder: (BuildContext context, snapshot) {
if (snapshot.hasError) {
errorMsg = snapshot.error.toString();
}
if (errorMsg != null) {
return Text(errorMsg);
}
return new Text(
snapshot.data.toString(),
);
},
)