flutter_bloc:使initialState方法异步 [英] flutter_bloc : Make initialState method async
问题描述
我正在使用flutter_bloc软件包来管理我的应用程序中的状态.我有一个用例,必须从远程数据库加载初始状态.这要求initialState方法是异步的,实际上不是.
I am using the flutter_bloc package to manage state in my app. I have a usecase where I have to load the initial state from the remote DB. This requires the initialState method to be async, which it is not.
如果不使用initialState方法,从远程数据库加载Bloc初始状态的最佳方法是什么?
If not by using the initialState method, what is the best way to load the initial state of a Bloc from a remote DB ?
推荐答案
您可以将event
发送到bloc
以开始加载(在该事件发生时,群发新的LoadingState
),您将在其中接收并显示bloc
发送另一个带有数据的状态,您只需将加载状态切换为已加载(并显示数据)即可. 您不需要等待呼叫,只需要做的就是推送和接收状态
You can send an event
to the bloc
to start loading(on it event bloc send new LoadingState
) where you receive and show Loader
, then when loading ended bloc
send another `state with data and you just switch loading state to loaded(and show data). You don't need to await call, what you have to do is just pushing and receiving states
这篇关于flutter_bloc:使initialState方法异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!