'Another exception was thrown: A AppNotifier was used after being disposed
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;
});
},
),
),
),
));
}
Solution 1:[1]
This error tells that you are using AppNotifier after disposal.
this is a temporary solution:
- remove:
appNotifier().dispose();
- or something like this:
@override
void dispose(){
AppNotifier().dispose(); //Remove This Code
super.dispose();
}
Solution 2:[2]
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.
- Use
ChangeNotifierProvider.valueto passwidget.appLanguage. - 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hamed |
| Solution 2 | Rahul |
