1 QuerySnapshot 在 Flutter Web 应用程序中返回 null 但在 Android 控制台中给出结果

1 QuerySnapshot returning null in Flutter web app but gives result in the Android console

我遇到一个问题,即在 android 控制台中打印时来自 firestore 的 QuerySnashot 给出正确的值,但在 flutter web 应用程序上构建它时 returns 为 null。有人可以协助我做错什么吗? 这是给我正确打印的函数:

countpassengers() async {
    String collection = "users";
    QuerySnapshot _myDoc = await firebaseFiretore.collection(collection).get();
    List<DocumentSnapshot> _myDocCount = _myDoc.docs;
    print("Number of Users: :::::::::::::");
    print(_myDocCount.length);  // Count of Documen
    totalusers = _myDocCount.length.toString() ;//ts in Collection
    print (totalusers.toString());
    return totalusers ;
}

这就是我如何调用它(返回 null)

InfoCard(
         title: "Number of Passengers",
         value: totalusers.toString(),//"3",
         onTap: () {},
         topColor: Colors.orange,
),

当您将函数的输出返回到 State 时,您需要通过调用 setState 函数重建状态。

通过调用 setState,整个 StatefulWidget 知道更新了一些东西并用新数据重建状态。因此,在 class 中调用 Firestore 函数后插入 setState。它可以在函数内部或 onTap: (){}.

内部

如果您在 onTap: (){} 中调用该函数,请不要忘记像这样添加异步:onTap: () async{}await 您的返回数据。