抛出另一个异常:空检查运算符用于空值
Another exception was thrown: Null check operator used on a null value
我需要使用未来的构建器从共享首选项中获取单选按钮的值。
但是当我打开单选按钮的页面时出现错误是:
Another exception was thrown: Null check operator used on a null value
单选按钮小部件是:
FutureBuilder<bool>(
future: getDayNotificationIsOn(),
builder: (context, snapshot) {
return NeumorphicSwitch(
value: snapshot.data!,
style: NeumorphicSwitchStyle(
thumbShape: NeumorphicShape.flat,
activeTrackColor: const Color(0xff257864),
),
onChanged: (value) async {
setState(() {});
//sharedPreferences
await setDayNotificationIsOn(value);
},
);
}),
未来builder获取单选按钮值的函数:
Future<bool> getDayNotificationIsOn() async {
SharedPreferences _pref = await SharedPreferences.getInstance();
return _pref.getBool('dayNotificationIsOn') ?? false;
}
我猜 snapshot.data
为 null 并且 snapshot.data!
正在触发此异常
您应该在 NeumorphicSwitch
之前添加一个检查,以通过添加 if (snapshot.hasData)
.
来确保快照有数据
我需要使用未来的构建器从共享首选项中获取单选按钮的值。 但是当我打开单选按钮的页面时出现错误是:
Another exception was thrown: Null check operator used on a null value
单选按钮小部件是:
FutureBuilder<bool>(
future: getDayNotificationIsOn(),
builder: (context, snapshot) {
return NeumorphicSwitch(
value: snapshot.data!,
style: NeumorphicSwitchStyle(
thumbShape: NeumorphicShape.flat,
activeTrackColor: const Color(0xff257864),
),
onChanged: (value) async {
setState(() {});
//sharedPreferences
await setDayNotificationIsOn(value);
},
);
}),
未来builder获取单选按钮值的函数:
Future<bool> getDayNotificationIsOn() async {
SharedPreferences _pref = await SharedPreferences.getInstance();
return _pref.getBool('dayNotificationIsOn') ?? false;
}
我猜 snapshot.data
为 null 并且 snapshot.data!
正在触发此异常
您应该在 NeumorphicSwitch
之前添加一个检查,以通过添加 if (snapshot.hasData)
.