Home > OS >  Another exception was thrown: A AppNotifier was used after being disposed
Another exception was thrown: A AppNotifier was used after being disposed

Time:04-26

I am new to Flutter. Im getting the following error messages after run my project "oops something wents wrong. please refresh the app or contact the administrator/developer" And i refresh the app after showing this error "A AppNotifier was used after being disposed. once you have called dispose() on a AppNotifier, it can no longer be used."

@override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: Size(360, 640),
        builder: () => ChangeNotifierProvider<AppNotifier?>(
              create: (_) => widget.appLanguage,
              child: Consumer<AppNotifier>(
                builder: (context, value, _) => MaterialApp(
                  navigatorKey: GlobalVariable.navState,
                  debugShowCheckedModeBanner: false,
                  locale: value.appLocal,
                  title: 'MyApp',
                  routes: <String, WidgetBuilder>{
                    'HomeScreen': (BuildContext context) => HomeScreen(),
                  },
                  theme: value.getTheme(),
                  supportedLocales: [
                    Locale('en', 'US'),
                    ],
                  localizationsDelegates: [
                    AppLocalizations.delegate,
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate,
                    GlobalCupertinoLocalizations.delegate,
                    CountryLocalizations.delegate,
                  ],
                  home: Builder(
                    builder: (context) {
                      return FutureBuilder(
                          future: DeeplinkConfig().initUniLinks(context),
                          builder: (_, snapshot) {
                            if (snapshot.connectionState ==
                                ConnectionState.waiting) {
                              return Container();
                            }
                            return snapshot.data as Widget;
                          });
                    },
                  ),
                ),
              ),
            ));
  }

CodePudding user response:

This error tells that you are using AppNotifier after disposal.

this is a temporary solution:

  1. remove:
appNotifier().dispose();
  1. or something like this:
@override
void dispose(){
  AppNotifier().dispose(); //Remove This Code

  super.dispose();
}

CodePudding user response:

You are not creating new object in ChangeNotifierProvider's create. You are passing already calculated value there. Hence it will be disposed when ChangeNotifierProvider's dispose is called internally.

You need to do 2 things.

  1. Use ChangeNotifierProvider.value to pass widget.appLanguage.
  2. You will have to manually dispose the value passed in widget.appLanguage. Maybe you can do it in the dispose of same widget where it's been initialised.

Thanks.

  • Related