简体   繁体   English

Flutter 显示键盘时再次调用 Bloc Builder

[英]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.我一直在使用 flutter 集团有一段时间了,我有一个问题,我不知道解决它的最佳方法是什么。 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) .这个小部件有一个输入文本字段和一个按钮,用于触发调用bloc.sendRequest(text)的网络请求。 The bloc emits a ResponseState(bool success, string message) depending on the server response. bloc 根据服务器响应发出一个ResponseState(bool success, string message) 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. Flutter 刷新构建器块,使用的 state 是前一个,它包含已经显示的错误消息,导致构建器再次显示弹出窗口。 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.如果 state 与之前相同,则向ResponseState添加时间戳,并且不要重建构建器。
  2. Make the bloc.sendRequest(text) call return the result and show the pop up if required once the Future is completed使bloc.sendRequest(text)调用返回结果,并在Future完成后根据需要显示弹出窗口
  3. Track which pop ups have been showed to avoid showing them twice using a timestamp in the ResponseState使用ResponseState中的时间戳跟踪已显示哪些弹出窗口以避免显示两次

What should be the best approach to solve this?解决这个问题的最佳方法应该是什么? I'm missing something?我错过了什么?

Thanks,谢谢,

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. BlocBuilder 可以随时重建,因此对于每个 state 只需触发一次的事件,最好使用BlocListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM