有人知道我该怎么做吗?
我的代码:
@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
。但是我找不到相应的处置方法。
任何帮助将不胜感激。谢谢!