firebase - 在 FLutter 中返回 null 的函数
问题描述
我正在尝试实现下面的功能,但它给了我空值。
具体来说,Future credit()
是不更新变量值。数据库没有问题,因为如果我把print(doc['value'])
而不是value += doc['value']
,我得到了预期的结果。看来,getCredit()
是返回null的那个。
Future credit() async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
final String uid = user.uid;
int value = 0;
Firestore.instance
.collection('entries')
.where("uid", isEqualTo: uid)
.where("type", isEqualTo: "+")
.orderBy("time", descending: true)
.snapshots()
.listen((data) => data.documents.forEach((doc) => value += doc['value']));
print(value); // doesnt update value
return value;
}
int getCredit() {
credit().then((value) {print(value);});
credit().then((value) {return value;}); // return mot working
}
谢谢!
解决方案
尝试这个:
Future<int> getCredit() async {
return await credit();
}
推荐阅读
- python - 如何使用 Matplotlib 获得 N 种易于区分的颜色
- azure - Azure Cosmos DB - '请求率很大。删除项目时可能需要更多请求单位的错误
- c# - C# API 调用另一个 api 失败,并显示“远程证书根据验证过程无效”
- python - 如何在 Python 的输入中读取多行?
- reactjs - react-leaflet hides a drop down menu
- xml - 删除具有等于某个值的后代的节点
- android - 在 Android Studio 中从数组中播放连续视频
- python - 如何检测图像偏移?
- openpyxl - 在openpyxl中插入行+移动单元格
- android - Android 10 不以编程方式连接到 WPA wifi