gpt4 book ai didi

flutter 错误 : A value of type 'Future' can't be assigned to a variable of type 'bool'

转载 作者:行者123 更新时间:2023-12-04 01:22:37 26 4
gpt4 key购买 nike

我正在尝试从 shared preferences 中读取,但我被卡住了。我有这个错误,我不知道如何处理它:A value of type 'Future<bool>' can't be assigned to a variable of type 'bool'
我的代码如下所示:

onTap: () {
setState(() {
if (_getPref()) { //here occurs the error
_stateColor = _disableColor;
_setPref(false);
} else {
_stateColor = _enableColor;
_setPref(true);
}
});
},

和方法:
Future<bool> _getPref() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool value = prefs.getBool(widget.myIndex) ?? false;
return value;
}

如果有人帮助我,我将不胜感激!

最佳答案

你必须 await _getPref() 函数,因为它返回一个 future 的 Future<bool>

onTap: () async {
if (await _getPref()) { //here occurs the error
_stateColor = _disableColor;
_setPref(false);
} else {
_stateColor = _enableColor;
_setPref(true);
}
setState(() {});
},

关于 flutter 错误 : A value of type 'Future<bool>' can't be assigned to a variable of type 'bool' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62410429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com