-
-
Notifications
You must be signed in to change notification settings - Fork 527
Closed
Labels
Description
The bug: Null check operator used on a null value
The following _CastError was thrown while dispatching notifications for ThemeProvider: Null check operator used on a null value
When the exception was thrown, this was the stack
#0 Element.widget
package:flutter/…/widgets/framework.dart:3203
#1 Provider._inheritedElementOf
package:provider/src/provider.dart:341
#2 Provider.of
package:provider/src/provider.dart:293
#3 AppTheme.init.<anonymous closure>
package:******/providers/theme_provider.dart:99
#4 ChangeNotifier.notifyListeners
package:flutter/…/foundation/change_notifier.dart:308
#5 ThemeProvider.setThemeMode
package:******/providers/theme_provider.dart:15
<asynchronous suspension>
The ThemeProvider sending notification was: Instance of 'ThemeProvider'
To Reproduce
theme_provider.dart
class ThemeProvider extends ChangeNotifier {
bool isDarkThemeEnabled = false;
setThemeMode(ThemeMode? themeMode) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
if (themeMode == ThemeMode.dark) {
isDarkThemeEnabled = true;
} else {
isDarkThemeEnabled = false;
}
await sharedPreferences.setBool("theme_mode", isDarkThemeEnabled);
notifyListeners();
print("Changing data $isDarkThemeEnabled");
}
}
class AppTheme {
.....
init(context) {
Provider.of<ThemeProvider>(context, listen: true).addListener(() {
themeMode =
Provider.of<ThemeProvider>(context, listen: false).isDarkThemeEnabled;
print("ThemeMode2: $themeMode");
....
});
}
main.dart
Future<void> setPrefThemeMode(BuildContext context) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var system_themeMode =
WidgetsBinding.instance?.window.platformBrightness == Brightness.dark;
var pref_themeMode =
(sharedPreferences.getBool("theme_mode") ?? system_themeMode);
var themeMode = system_themeMode ? ThemeMode.dark : ThemeMode.light;
print("systeMode:$system_themeMode");
if (system_themeMode == false && pref_themeMode == true) {
//themeMode = ThemeMode.dark;
}
Provider.of<ThemeProvider>(context, listen: false).setThemeMode(themeMode);
}
class MyApp extends StatefulWidget {
//const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
// connect();
super.initState();
WidgetsBinding.instance?.addObserver(this);
WidgetsBinding.instance?.addPostFrameCallback((_) {
setPrefThemeMode(context);
});
}
@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
super.dispose();
}
@override
void didChangePlatformBrightness() {
super.didChangePlatformBrightness();
print("ThemeModeChanged");
setPrefThemeMode(context);
}
@override
Widget build(BuildContext context) {
AppTheme().init(context);
return MaterialApp(debugShowCheckedModeBanner: false, home: SplashScreen());
}
}
Expected behavior
The isDarkThemeEnabled boolean value must return the correct value and be applied to the var themeMode in AppTheme class for further changes.
Reactions are currently unavailable