Flutter Bloc Builder is called again when the keyboard is shown


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:

  1. Add a timestamp to the ResponseState and do not rebuild the builder if the state is the same as before.
  2. Make the bloc.sendRequest(text) call return the result and show the pop up if required once the Future is completed
  3. Track which pop ups have been showed to avoid showing them twice using a timestamp in the ResponseState

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 BlocListener instead.

Answered By – Joan P.S

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *