【发布时间】:2021-10-02 11:40:59
【问题描述】:
我有一个使用 Bloc 模式进行状态管理的 Flutter 应用。
我在存储状态的页面中有一个局部变量。
PageA:
// Outside build function
List<String> _names = []; // local variable to show the changes visually
.
.
.
// Inside build function
return Scaffold(
body: BlocConsumer<PageABloc, PageAState>(
builder: (context, state) {
if (state is PageASuccessState) {
_names = state.names; // here is problem
return _body();
} else if (state is PageALoadingState ||
state is PageAInitState) {
return Center(child: CircularProgressIndicator());
} else
return Center(child: Text("Something went wrong"));
},
listener: (context, state) {},
),
);
当用户点击_body()中的保存按钮时,我只需要更新state.names的状态,但要向用户显示视觉变化,我正在使用_names 局部变量。
如何将state.names的初始值加载到_names?
我尝试的代码不起作用,因为它会在每一帧上重置对 _names(local) 的任何更改。
我试过类似的东西,
if (state is PageASuccessState) {
final List<String> _n = state.names;
_names.addAll(_n);
return _body(_width);
}
但这只是将state.names 重复添加到_names,无限次。
救命!
【问题讨论】:
标签: flutter bloc flutter-bloc