does anyone have any idea of how I can do this?
My code:
@override
void dispose() {
final FiltersBloc filtersBloc =
BlocProvider.of<FiltersBloc>(context);
super.dispose();
}
error is:
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)
Also, if I follow the error code and use final filtersBloc = BlocProvider<FiltersBloc>(builder: (context) => FiltersBloc())
instead, I cannot call filtersBloc.dispatch()
anymore.
I know for initState, we can just didChangeDependencies
instead. But I cannot find an equivalent for dispose.
Any help would be greatly appreciated. Thanks!
final FiltersBloc filtersBloc = BlocProvider.of<FiltersBloc>(context);
in a parent widget then pass it as a parameter so that on dispose, you can usewidget.filtersBloc.dispatch();
– Willywillynilly