tyler powell
tyler powell

Reputation: 130

Method [] was called on null only occuring on IOS

I am aware that there are many questions like this one already floating around on StackOverFlow. I have noticed however that my condition is very unique. I am using a stream builder to get data about a user from my firebase console. I then use this snapshot to query the data and use it. What makes my circumstance unique is that the error only occurs when the application is run on IOS.

The stream builder is as follows, and to my knowledge checks everything that could make the snapshot 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();
  }

The error is then returned when attempting to use the data in this method:

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);
                                }),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

The strangest part about the code is that the values in the getCard method are actually displayed on the screen as if there is no error getting them at all. The app works fine on android and only crashes when using ios.

Upvotes: 0

Views: 87

Answers (1)

Henok
Henok

Reputation: 3383

snapshot.connectionState != ConnectionState.waiting returns true for both ConnectionState.none as well, so instead try if(snapshot.connectionState == ConnectionState.waiting)return yourLoadingWIdget; else .... your code;

Upvotes: 1

Related Questions