Justin Waweru
Justin Waweru

Reputation: 33

How can i filter firestore data by a field in another collection in flutter

database viewI have tried to assign the field value to a local variable but its not working

getData() async {
        String userId = 'userId';
        Firestore.instance.collection('user').document(userId).snapshots();
        var snapshot;
        var userDocument = snapshot.data;
        String _myAddress = userDocument["address"];
        return Firestore.instance
            .collection('letters')
            .where("source Box", isEqualTo: _myAddress)
            .snapshots();
      }

Upvotes: 1

Views: 224

Answers (1)

Henok
Henok

Reputation: 3393

You should make your question more clear but I have tried fixing some obvious problems with your code.

getData() async {
        String userId = 'userId';
        var userDocument = await Firestore.instance.collection('user').document(userId).get();


        String _myAddress = userDocument["address"];
        return Firestore.instance
            .collection('letters')
            .where("source Box", isEqualTo: _myAddress)
            .snapshots();
      }
  • You can get a single document if you know the document Id by calling get() directly
  • You weren't calling await for asynchronous requests for next time you must wait for such Futures in order to get the result. To understand more about async/await I recommend reading this and more on the topic.

Upvotes: 1

Related Questions