如何解决Bloc-是否可以产生导航堆栈中上一页的状态?
我有一个BlocBuilder,它根据我的仪表板页面的产生状态来处理建筑小部件。
body: BlocBuilder<DashboardBloc,DashboardState>(
builder: (context,state) {
print(state);
if (state is DashboardInitial) {
return loadingList();
} else if (state is DashboardEmpty) {
return emptyList();
} else if (state is DashboardLoaded) {
return loadedList(context,state);
}
},),floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,MaterialPageRoute(builder: (context) => AddPage()));
},
我希望能够导航到添加页面,填写一些文本字段,然后将事件发送到我的仪表板块,其想法是在导航回仪表板后,我的列表将被更新。
class AddPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
TextEditingController titleController = TextEditingController();
TextEditingController descriptionController = TextEditingController();
return Scaffold(
appBar: AppBar(title: Text('Add')),body: Container(
padding: EdgeInsets.all(10),child: Column(
children: [
TextField(
controller: titleController,TextField(
controller: descriptionController,RaisedButton(onPressed: () {
BlocProvider.of<DashboardBloc>(context)
.add(DashboardWorryAdded('title','description'));
}),],);
}
}
使用断点遵循代码时,我可以看到我的状态在'mapeventtostate'函数中产生,但是我的仪表板从未用新值重建。
这是我的Bloc,事件和状态的代码。我首先想到的是,Equatable正在检测返回的相同状态,但是在删除Equatable时,我的问题仍然存在。
@override
Stream<DashboardState> mapEventToState(
DashboardEvent event,) async* {
if (event is DashboardWorryAdded) {
yield* _mapDashboardWorryAddedToState(event);
} else if (event is DashboardLoading) {
yield* _mapDashboardLoadingToState(event);
} else if (event is AppStarted) {
yield* _mapAppStartedToState(event);
}
}
Stream<DashboardState> _mapAppStartedToState(AppStarted event) async* {
List<Worry> _wList = await repo.getAllWorries();
if (_wList.length != 0) {
yield DashboardLoaded(worryList: _wList);
} else {
yield DashboardEmpty();
}
}
Stream<DashboardState> _mapDashboardLoadingToState(
DashboardLoading event) async* {
List<Worry> _wList = await repo.getAllWorries();
if (_wList != 0) {
yield DashboardLoaded(worryList: _wList);
} else {
yield DashboardEmpty();
}
}
Stream<DashboardState> _mapDashboardWorryAddedToState(
DashboardWorryAdded event) async* {
await repo.addWorry(event.title,event.description);
List<Worry> worryList = List<Worry>();
worryList = await repo.getAllWorries();
yield DashboardLoaded(worryList: worryList);
}
}
@immutable
abstract class DashboardEvent {}
class DashboardLoading extends DashboardEvent {
DashboardLoading();
}
class DashboardWorryAdded extends DashboardEvent {
final String title,description;
DashboardWorryAdded(this.title,this.description);
}
class AppStarted extends DashboardEvent {
AppStarted();
}
@immutable
abstract class DashboardState {}
class DashboardInitial extends DashboardState {
DashboardInitial();
}
class DashboardLoaded extends DashboardState {
final List<Worry> worryList;
DashboardLoaded({this.worryList});
}
class DashboardEmpty extends DashboardState {
DashboardEmpty();
}
解决方法
不要试图改变另一个页面的状态(在涉及状态管理的地方要注意一点点),请利用导航器的push
方法返回一个将来该页面完成的未来这一事实。被弹出,作为奖励,future的值将包括在另一页中为pop
方法赋予的值。因此,您现在可以执行以下操作:
class DashboardBloc {
...
void showAddPage() async {
// Do this to retrieve the value passed to the add page's call to `pop`
final value = await Navigator.of(context).push(...);
// Do this if the add page doesn't return a value in `pop`
await Navigator.of(context).push(...);
// Either way,you can now refresh your state in response to
// the add page popping
emit(...);
}
}
注意:这也适用于命名路由。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。