简体   繁体   English

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

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

I am having an issue where the QuerySnashot from the firestore giving correct value when printing in the android console but returns a null when building it on the flutter web app.我遇到一个问题,即在 android 控制台中打印时来自 firestore 的 QuerySnashot 提供正确的值,但在 flutter web 应用程序上构建它时返回 null。 Can someone please assist on what I am doing wrong?有人可以协助我做错什么吗? Here is the function giving me the correct print:这是给我正确打印的 function:

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 ;
}

and here is how I am calling it (returning null) somehow这就是我以某种方式调用它(返回 null)的方式

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

When you are returning the output from a function to State you need to rebuild the state by calling setState function.当您将 output 从 function 返回到State时,您需要通过调用setState function 重建 state。

By calling setState, the whole StatefulWidget knows something is updated and rebuild the state with new data.通过调用 setState,整个 StatefulWidget 知道更新了一些东西,并用新数据重建 state。 So insert setState after calling your Firestore function in the class. It could be inside a function or inside onTap: (){} .因此,在 class 中调用 Firestore function 后插入 setState。它可能在 function 内或onTap: (){}

If you call that function inside your onTap: (){} , don't forget to add async like that: onTap: () async{} and await your returning data.如果您在onTap: (){}中调用 function,请不要忘记像这样添加异步: onTap: () async{}await您返回的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM