【发布时间】:2021-02-15 22:25:09
【问题描述】:
我正在尝试获取开关状态,但它一直向我显示此错误 断言失败:布尔表达式不能为空
这就是我如何通过将开关状态保存到共享首选项并取回值来获得它
bool availability;
Future<bool> getSwitchState() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
availability = prefs.getBool('isAvailable') ??false;
});
}
// and this how i save the switch state when it is changed
Future<bool> savingSwitchValue(bool value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setBool('isAvailable', value);
}
CustomSwitch(
value: availability,
onChanged: (value) {
if (availability == false) {
setState(() {
availability = true;
savingSwitchValue(value);
print('this is true');
goOnline();
getLocationUpdates();
});
} else {
setState(() {
availability = false;
savingSwitchValue(value);
print('this is false');
goOffline();
});
}
},
activeColor: Colors.green,
),
【问题讨论】:
标签: android ios flutter mobile-application