【发布时间】:2021-05-05 02:52:40
【问题描述】:
我正在使用 firebase cloud firestore
在 Future 函数中我有这个
try {
categories.forEach((element) async {
await FirebaseFirestore.instance.collection('Categories').add({
'name': element[0],
'imageUrl': element[1],
});
print('done');
});
print('complete');
} catch (e) {
CoolAlert.show(
context: context,
type: CoolAlertType.error,
content: Text(e),
text: "Upload Failed",
onConfirmBtnTap: () {
Navigator.pop(context);
Navigator.pop(context);
});
}
在“完成”之前打印“完成” 如何使它相反? 如何等待 forEach 函数先结束然后继续
即使我在整个 try catch 块之后移动了print('complete'); ,它也不起作用
那么有没有办法等待 try catch 块?
【问题讨论】:
-
不确定,但您是否尝试将 await 放在 categories.forEach((element) 之前?
-
是的,它说“‘await’应用于‘void’,而不是‘Future’”