首页 > 解决方案 > 在 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
  }

谢谢!

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


尝试这个:

Future<int> getCredit() async {
  return await  credit();
}

推荐阅读