【发布时间】:2020-02-16 06:26:43
【问题描述】:
有人知道我该怎么做吗?
我的代码:
@override
void dispose() {
final FiltersBloc filtersBloc =
BlocProvider.of<FiltersBloc>(context);
super.dispose();
}
错误是:
flutter: BlocProvider.of() called with a context that does not contain a Bloc of type FiltersBloc.
flutter: No ancestor could be found starting from the context that was passed to
flutter: BlocProvider.of<FiltersBloc>().
flutter:
flutter: This can happen if:
flutter: 1. The context you used comes from a widget above the BlocProvider.
flutter: 2. You used MultiBlocProvider and didn't explicity provide the BlocProvider types.
flutter:
flutter: Good: BlocProvider<FiltersBloc>(builder: (context) => FiltersBloc())
flutter: Bad: BlocProvider(builder: (context) => FiltersBloc()).
flutter:
flutter: The context used was: FiltersDrawer(dirty, state: _FiltersDrawerState#86e8a)
另外,如果我按照错误代码改用final filtersBloc = BlocProvider<FiltersBloc>(builder: (context) => FiltersBloc()),我将无法再调用filtersBloc.dispatch()。
我知道对于 initState,我们可以改为 didChangeDependencies。但我找不到 dispose 的等价物。
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
你好,魏。为什么你需要让 Bloc 内部处置?你想在这里处理 Bloc 对象吗?
-
不,我想在处理小部件时向我的集团分派一个事件
-
如何在父窗口小部件中声明
final FiltersBloc filtersBloc = BlocProvider.of<FiltersBloc>(context);,然后将其作为参数传递,以便在处理时可以使用widget.filtersBloc.dispatch(); -
谢谢,我从这里得到了答案:github.com/felangel/bloc/issues/588。基本上就是你说的。谢谢