【发布时间】:2020-07-25 07:19:45
【问题描述】:
我正在尝试在 setState 块内运行带有 await 的语句,我将它添加到另一个 Future<void> 函数中,但我仍然需要在 setState 上添加 async 才能运行它。
这是我的代码:
setState(() async {
chosenStations.clear();
chosenStations.add(allStations[
suggestions.indexOf(fromLocationName)]);
_loading = true;
chosenStations.add(allStations[
suggestions.indexOf(toLocationName)]);
await showingLines();
});
Future<void> showingLines() async {
theLines.clear();
theLines = await DatabaseServices().fetchingLinesData(
chosenStations[0], chosenStations[1]);
}
我得到了这个错误:
Instead of performing asynchronous work inside a call to setState(), first execute the work (without updating the widget state), and then synchronously update the state inside a call to setState().
【问题讨论】:
标签: android ios flutter asynchronous dart