【发布时间】:2021-10-16 00:18:49
【问题描述】:
我试图在这个函数中返回一个 int:
Future<int> getID() async {
DocumentSnapshot idSnapshot = await FirebaseFirestore.instance
.collection("Bestellungen")
.doc("default")
.get();
int currentID = idSnapshot["id"] + 1;
return await currentID;
}
现在我想在我的 GestureDetector 的 onTap 方法中使用它:
onTap: () {
Scaffold.of(context).hideCurrentSnackBar();
FirebaseFirestore.instance
.collection("Bestellungen")
.add({
"id": getID(),
"item": document["titel"],
"done": false,
"time": getTime(),
});
}
但是,如果我点击我的按钮,应用程序会崩溃,并在 VS Code 中显示以下内容:
我认为问题与 Future、async 和 await 相关。 我还尝试在 getID() 之前设置等待,但为此还需要异步,但如果方法正确,我不知道将它包含在我的 Stateful 类中的哪个位置。
感谢您的帮助。
【问题讨论】:
-
您能向我们展示您获得的确切错误消息和完整的堆栈跟踪,而不是向我们展示调试器的屏幕截图吗?
标签: flutter google-cloud-firestore async-await