0
votes

Very strange behaviour I am experiencing with firestore.

Below dart code does not return the value

await Firestore.instance.collection('users')
          .where("phoneNumber", isEqualTo: phoneNumber)
          .getDocuments();

The javascript code from web returns the value

db.collection('users').where('phoneNumber', '==', 'xxxxxxxxxx').get().then((result) => {
       console.log(  result.docs.length )
}).catch((err) => {
console.log(err)
});

But I can clearly see the phone number does exist in the colection. I just don't know if this is because of pending writes or cache. Where can I disable it if that is the case?

edit the code for phNumber

Future<User> getPhoneUser(String phoneNumber) async {
    if (phoneNumber == 'xxxxxxxxxx') {
      print('yes the phone number is same');
    }
    try {
      QuerySnapshot qsnap = await usersRef
          .where("phoneNumber", isEqualTo: phoneNumber)
          .getDocuments();

      int length = qsnap.documents.length;

      if (length > 0) {
        DocumentSnapshot doc = qsnap.documents[0];
        doc.data['id'] = doc.documentID;
        doc.data['createdAt'] = doc.data['createdAt'].toDate().toString();
        doc.data['updatedAt'] = doc.data['updatedAt'].toDate().toString();
        User user = User.fromJson(doc.data);
        return user;
      } else {
        return null;
      }
    } catch (error) {
      return null;
    }
  }
1
are u sure phoneNumber is not null? - Peter Haddad
@PeterHaddad , yes the phonenumber is not null. There is not much code apart from this call. ```` var db = firebase.firestore(); var Timestamp = firebase.firestore.Timestamp; ```` - p2hari
Where are u assigning the phoneNumber - Peter Haddad
I edited with the code for phoneNumber. I am also getting the print statement in the console. :) - p2hari

1 Answers

0
votes

If you are running your flutter app on a simulator, sometimes the simulators go offline for some reason and you can no longer fetch data from the network.

If this happens, you can restart your simulator to bring the network connection back. I would suggest debugging with an actual physical device whenever possible.