Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access BlocProvider.of<Bloc>(context) in dispose method

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!

like image 334
wei Avatar asked Oct 15 '22 10:10

wei


1 Answers

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();
}
like image 77
Omatt Avatar answered Oct 20 '22 08:10

Omatt