HomeiOS Developmentandroid - Why I am unable to present snackbar utilizing a world...

android – Why I am unable to present snackbar utilizing a world scaffold key after dispose?


I’ve a world key assigned to the highest stage scaffold in my app:

ultimate GlobalKey<ScaffoldState> globalScaffoldKey = GlobalKey();

I assign it like this:

             MaterialApp.router(
              debugShowCheckedModeBanner: false,
              routerConfig: appRouter,
              builder: (context, youngster) {
                return Container(
                    youngster: Scaffold(
                      key: globalScaffoldKey,
                      physique: ...
                    ),
                  ),
                );
              },

Then in one of many screens under the fabric app, I’ve this code:

                      doSomeAsyncWork(
                        onSuccess: () {
                          showSuccessSnackBar(
                            context: globalScaffoldKey.currentContext!,
                            textual content: AppLocalizations.of(context)!
                                .profileUpdatedSuccessfully,
                          );
                          if(!mounted) return;
                          GoRouter.of(context).pop();
                        },
                      );

If I’m going again from that display screen earlier than the onSuccess has been known as (it’s known as from inside doAsyncWork), then when its known as, I get this error: This widget has been unmounted, so the State now not has a context (and ought to be thought-about defunct

I do know it implies that the globalScaffoldKey.currentContext! is being thought-about not mounted, however my query is why? That scaffold is above each the display screen I used to be in and the display screen I navigated to.

word: – showSuccessSnackBar simply calls the built-in ScaffoldMessenger.of(context).showSnackBar:

  ScaffoldMessenger.of(context).clearSnackBars();
  return ScaffoldMessenger.of(context).showSnackBar(
...

I even tried calling the showSnackBar methodology from the get rid of this display screen after 2 seconds:

  @override
  void dispose() {

     ...

    Future.delayed(
      Length(seconds: 2),
      () {
        print('after 2 seconds of disposing profile display screen');
        print('is display screen mounted: $mounted');
        print('is world scaffold key mounted: ${globalScaffoldKey.currentContext!.mounted}');
        return showSuccessSnackBar(
        context: globalScaffoldKey.currentContext!,
        textual content: AppLocalizations.of(context)!.profileUpdatedSuccessfully,
      );
      },
    );

I obtained this output (and the identical error)

flutter: is display screen mounted: false
flutter: is world scaffold key mounted: true

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments