【发布时间】:2022-01-06 17:14:59
【问题描述】:
当我点击按钮更改应用主题时,出现此错误:
The following _CastError was thrown while handling a gesture:
Null check operator used on a null value
这是我的代码,我正在使用 ThemeSwitcher 包:
AppBar buildAppBar(BuildContext context, bool isEdit) {
final icon = CupertinoIcons.moon_stars;
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
return AppBar(
leading: IconButton(
onPressed: () {
if (isEdit == true) Navigator.pop(context);
},
icon: Icon(Icons.arrow_back),
),
backgroundColor: Colors.transparent,
elevation: 0,
actions: [
ThemeSwitcher(
clipper: ThemeSwitcherBoxClipper(),
builder: (context) => IconButton(
icon: Icon(icon),
// Button to change theme
onPressed: () {
final theme = isDarkMode ? Themes.lightTheme : Themes.darkTheme;
// originally, there is a '!' on the switcher context below, but it says switcher context can never be null.
final switcher = ThemeSwitcher.of(context);
switcher.changeTheme(theme: theme, isReversed: false); //this is the code that the error pointed.
},
),
),
],
);
}
那么我该如何解决这个错误呢?我通过观看 youtube 上的教程获得了这段代码,并且我编写了完全相同的代码。
【问题讨论】:
-
能否附上包链接
-
我使用了 animated_theme_switcher:^2.0.6。但现在已经解决了。原来我从错误的包中获得了 main.dart 中的 ThemeProvider。我想从动画主题切换器中选择,但我从 ThemeProvider Package 中选择了 ThemeProvider。
标签: flutter dart themes flutter-dependencies