【发布时间】:2021-07-07 06:31:27
【问题描述】:
假设我想从后端服务器获取数据,并在 initState 中通过 post 方法调用 API。下面的函数我想一一调用。
Map response = {};
Map response2 = {};
void initState() {
callAPI_1();
callAPI_2();
ShowData();
}
void callAPI_1() async {
.
.
.
HttpClientResponse responseBody = await request.close();
this.setState((){
response = responseBody;
});
}
void callAPI_2() async {
.
.
.
HttpClientResponse responseBody2 = await request2.close();
this.setState((){
response2 = responseBody2;
});
}
void ShowData() {
print(response);
print(response2);
}
预计编程流程的顺序应该是initState -> callAPI_1 -> callAPI_1 -> ShowData;
关于如何实现它的任何建议?
【问题讨论】:
-
创建另一个异步函数并从 initState 中调用它,类似这样