I’ve been using flutter blocs for a while and I have a problem which I don’t know what would be the best approach to solve it.
I have a Widget that uses a bloc. This widget has an input text field and a button which fires a network request calling
bloc.sendRequest(text). The bloc emits a
ResponseState(bool success, string message) depending on the server response. If there is an error the bloc builder will show a pop up displaying the error message and asking the user to change the input field.
The problem comes when the user press the text input after the error pop up is shown. Flutter refresh the builder bloc and the used state is the previous one, which it cointains the error message that has been already shown, causing the builder to show again the pop up. What should be the best approach to tackle this situation? I’ve thought about some solutions:
- Add a timestamp to the
ResponseStateand do not rebuild the builder if the state is the same as before.
- Make the
bloc.sendRequest(text)call return the result and show the pop up if required once the
- Track which pop ups have been showed to avoid showing them twice using a timestamp in the
What should be the best approach to solve this? I’m missing something?
BlocBuilder can rebuild at any time so for events that need to be fired only once per state it is better to use
Answered By – Joan P.S
Answer Checked By – Marie Seifert (FlutterFixes Admin)