我正在尝试实现以下功能,但它为我提供了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();
}
关于firebase - 函数在FLutter中返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61914088/