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!
BlockProvider needs context to be initialized. You may initialize it on the screen's Widget build()
late FiltersBloc filtersBloc;
@override
Widget build(BuildContext context){
filtersBloc = BlocProvider.of<FiltersBloc>(context);
return ...
}
...then call the desired method on dispose()
@override
void dispose() {
filtersBloc.dispatch();
super.dispose();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With