简体   繁体   English

Flutter:在 Bloc 转换之间的小部件中保存状态

[英]Flutter : Saving state in a widget between Bloc transitions

I am using flutter_bloc to implement bloc in my app.我正在使用flutter_bloc在我的应用程序中实现 bloc。

I have a screen which does multiple transitions.我有一个可以进行多次转换的屏幕。 So multiple events are dispatched on different actions on the page and each state returned represents the state in return for that specific event.因此在页面上的不同操作上分派多个事件,返回的每个状态代表该特定事件的返回状态。

How do I maintain state in my widget in between these transitions ?如何在这些转换之间维护我的小部件中的状态?

For example, the screen has 3 buttons.例如,屏幕有 3 个按钮。 Each button click dispatches an event and the state returns a piece of text.每次单击按钮都会调度一个事件,状态会返回一段文本。 So after clicking button1, the state returns a State object with the text and the button is replaced by this text.所以点击 button1 后,状态返回一个带有文本的 State 对象,按钮被这个文本替换。 Now when the user clicks button2, it returns another piece of text and it replaces button2.现在,当用户单击 button2 时,它会返回另一段文本并替换 button2。 But when this happens, button1's text disappears.但是当发生这种情况时,button1 的文本就会消失。

I have a StatefulWidget with state which looks like this -我有一个 StatefulWidget,其状态如下所示 -

List<String> allTexts = List();

but updating this state is not working as calling setState throws this error -但更新此状态不起作用,因为调用 setState 会引发此错误 -

setState() or markNeedsBuild() called during build.

How do I solve this problem ?我该如何解决这个问题?

Thanks in advance.提前致谢。

You can always emit the State with the text for all of the buttons .您始终可以为所有buttons发出带有文本的State So when you click button 2, you receive the text of button 2 + all the text saved for button 1 and 3因此,当您单击button 2 时,您会收到button 2 的text + 为button 1 和 3 保存的所有text

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

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