【发布时间】:2020-10-25 12:28:12
【问题描述】:
我收到此错误
“未来”类型的值不能分配给 “双”类型的变量。 因为我们不能在 initState() 方法中使用异步。
class _SettingsState extends State<Settings> {
bool _isSwitched = false;
double _sliderValue = 0;
@override
void initState() async {
_sliderValue = _getInterval();
super.initState();
}
...
Future<double> _getInterval() async { ...
但是如果我们尝试在 initState() 中使用 await,那么我们会收到消息
而不是直接在内部等待异步工作 initState,调用一个单独的方法来完成这项工作而无需等待 它。
如何在 initState 中使用 _getInterval,即从它接收未来值,而不等待它?
【问题讨论】:
-
然后使用
Future.then()方法 -
@Rozerro 你得到答案了吗?
标签: flutter