Flutter从api获得答案后移动到下一行代码



我有一个调用4个api的函数。

getDefects();
getInstruction();
getToDone();
getToInvoice();

之后它会更新我的app的UI

status.success();
update(['all']);

我希望这个函数一起调用所有4个api,而不是等待他们的答案,但是当所有4个api的答案回来时,它移动到下一部分更新UI的代码

getData() async {
status.loading();
try {
getDefects();
getInstruction();
getToDone();
getToInvoice();

status.success();
update(['all']);
} catch (e) {
status.error(e.toString());
}
}
updateState(int count) {
if (count == 4) {
status.success();
update(['all']);
}
}
getData() async {
status.loading();
int count = 0;
try {
getDefects().then(value => updateState(count++);
getInstruction().then(value => updateState(count++);
getToDone().then(value => updateState(count++);
getToInvoice().then(value => updateState(count++);
} catch (e) {
status.error(e.toString());
}
}

最新更新