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

2

12

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!

Modillion answered 19/10, 2019 at 14:30 Comment(4)
hello wei. why would you need to get the Bloc inside dispose? do you want to dispose the Bloc object here?Willywillynilly
No, I want to dispatch an event to my bloc when dispose of the widgetModillion
how about declare the final FiltersBloc filtersBloc = BlocProvider.of<FiltersBloc>(context); in a parent widget then pass it as a parameter so that on dispose, you can use widget.filtersBloc.dispatch();Willywillynilly
Thanks, i got my answer from here: github.com/felangel/bloc/issues/588. It's basically what you said. ThanksModillion
S
13

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();
}
Supplicate answered 13/10, 2021 at 14:34 Comment(3)
You should point out that late is only for projects which are null safety.Categorize
this answer is not correct, didChangeDependencies should be used instead of build methodCarycaryatid
@Carycaryatid : why do you suggest didChangeDependencies? you mean because of this explanationAssumed
C
0

A small more detailed approach:

sound null safety, you can use @Omatt answer.

unsound null safety, you can use the following approach:

Declare a static variable inside the class and assign it inside init or build as follows:

class MyAwesomeWidget extends StatefulWidget {
  const MyAwesomeWidget({
    Key key,
  });

  @override
  _MyAwesomeWidgetState createState() => _MyAwesomeWidgetState();
}

class _MyAwesomeWidgetState extends State<MyAwesomeWidget> {
  FiltersBloc filtersBloc;

  @override
  void initState() {
    super.initState();
    new Future.delayed(Duration.zero, () {
      filterBloc = BlocProvider.of<FiltersBloc>(context);
    });
  }

  @override
  void dispose() {
    filterBloc.myFunction();
    super.dispose();
  }
}

Categorize answered 23/12, 2021 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.