我知道有很多这样的问题已经在StackOverFlow上浮现了。但是我注意到我的情况非常独特。我正在使用流构建器从Firebase控制台获取有关用户的数据。然后,我使用此快照查询数据并使用它。使我的情况与众不同的是,该错误仅在应用程序在IOS上运行时发生。
流构建器如下,据我所知,检查了所有可能使快照为null的内容:

Widget getPostItem(DocumentSnapshot doc) {
    return new FutureBuilder(
        future: getOtherUserDoc(doc["uid"]),
        builder: (BuildContext context, snapshot) {
          List<Widget> children;
          if (snapshot.hasData && !snapshot.hasError && snapshot.data["lat"] != null && snapshot.connectionState != ConnectionState.waiting) {
            return getCard(doc, snapshot);
          } else {
            return Center(child: CircularProgressIndicator());
          }
        });
  }

Future<dynamic> getOtherUserDoc(uid) async {
    return await Firestore.instance.collection("users").document(uid).get();
  }
尝试在此方法中使用数据时,将返回错误:
Widget getCard(doc, snapshot) {

    print("getting the value of lat and long");
    double useLat = double.parse(snapshot.data["lat"].toString());
    double useLong = double.parse(snapshot.data["long"].toString());

    double distance = roundDouble(
        distanceBetween(widget.lat, widget.long, useLat, useLong) *
            0.000621371192,
        2);

    return Card(
      child: SizedBox(
        width: MediaQuery.of(context).size.width,
        child: new Column(
          children: <Widget>[
            Expanded(
              child: ListTile(
                title: new ListView(shrinkWrap: true, children: <Widget>[
                  new Text(
                    snapshot.data["nameF"] + ", " + doc["age"],
                    style: TextStyle(fontSize: 40, color: Colors.red),
                  ),
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.location_on,
                        color: Colors.red,
                      ),
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          snapshot.data["city"] + ", " + snapshot.data["state"],
                          style: TextStyle(
                            fontSize: 16,
                          ),
                          textAlign: TextAlign.left,
                        ),
                      )
                    ],
                  ),
                  Text(
                    distance.toString() + " miles away",
                    style: TextStyle(
                      fontSize: 16,
                    ),
                    textAlign: TextAlign.left,
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                    child: new Container(
                      width: double.infinity,
                      height: 1,
                      color: Colors.grey,
                    ),
                  ),
                  new Text(
                    "Bio:",
                    style: TextStyle(fontSize: 32, color: Colors.red),
                  ),
                  Container(
                    width: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(
                        border: Border.all(
                          color: Colors.red,
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(20))),
                    child: Padding(
                      padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
                      child: new Text(
                        doc["bio"],
                        style: TextStyle(fontSize: 26),
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "Interests: ",
                        style: TextStyle(
                          fontSize: 22,
                          color: Colors.red,
                        ),
                        textAlign: TextAlign.left,
                      ),
                    ),
                  ),
                  getInterestGridView(snapshot.data),
                ]),
              ),
            ),
            new Padding(
              padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
              child: Align(
                alignment: Alignment.topLeft,
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          SizedBox(
                            width: MediaQuery.of(context).size.width / 2 - 4,
                            height: MediaQuery.of(context).size.width / 5,
                            child: Container(
                              color: Colors.red,
                              child: SizedBox(
                                width:
                                    MediaQuery.of(context).size.width / 2 - 4,
                                height: MediaQuery.of(context).size.width / 5,
                                child: IconButton(
                                    icon: Icon(
                                      Icons.clear,
                                      size:
                                          MediaQuery.of(context).size.width / 6,
                                    ),
                                    color: Colors.black,
                                    onPressed: () {
                                      rejectProfile(doc);
                                    }),
                              ),
                            ),
                          ),
                          SizedBox(
                            width: MediaQuery.of(context).size.width / 2 - 4,
                            height: MediaQuery.of(context).size.width / 5,
                            child: FlatButton(
                                child: Image.asset("assets/blindlogo.png"),
                                color: Colors.black,
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(0.0),
                                    side: BorderSide(color: Colors.black)),
                                onPressed: () {
                                  acceptProfile(doc);
                                }),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
关于代码的最奇怪的部分是,getCard方法中的值实际上显示在屏幕上,好像完全没有错误。该应用在Android上运行良好,并且仅在使用ios时崩溃。

最佳答案

snapshot.connectionState != ConnectionState.waiting也为ConnectionState.none都返回true,因此请尝试if(snapshot.connectionState == ConnectionState.waiting)return yourLoadingWIdget; else .... your code;

关于firebase - 方法[]仅在IOS上空调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64309949/

10-10 18:06