1
votes

I'm trying to query a document field from a different firestore collection than the one of the streambuilder. Right now, with the streambuilder I am able to query one of the documents i need, however I don't how i'm to query another collection in that same stream builder. I thought about using a global variable from another class where I had already queried the document I want but that didn't seem to work.

This is the stream builder

Container(
              height: MediaQuery.of(context).size.height,
              margin: EdgeInsets.only(top: 0),
              padding: EdgeInsets.only(top: 0),
              child: StreamBuilder(
                  stream: Firestore.instance
                      .collection('Tournaments')
                      .document('Fortnite Tournaments')
                      .collection('Fortnite Tourneys')
                      .document(docID)
                      .snapshots(),
                  builder: (context, snapshot) {
                    if (!snapshot.hasData) {
                      return new Text("Loading");
                    }
                    var tourneyDetails = snapshot.data;
                    return ListView(
                      children: <Widget>[ 
                            ..........

The collection i'm trying to query is called "users"
Why i need to query a field in a different collection is because i'm writing an if statement that needs both of the fields

if (int.parse("Field_i_am_trying_to_get_from_another_collection") > int.parse(tourneyDetails[ 'tourneycost']))
                               print('You have the money');                                                   
                                       else {
                                      print('You're broke');
                                        }

That condition is going to be executed when the raised button is clicked. Essentially, My problem is that I am currently not sure how I can query the data from that other collection.

Not sure if i explained that well but comment if you need more context or code, Thanks.

1
You can nest a streambuilder that queries the second collection inside the ListView - ByteMe

1 Answers

0
votes

You can use provider instead of creating global variable. see Provider package: https://pub.dev/packages/provider

if the current user is the user you want, then you can take the current user data from firestore when the app is loading and provide the user to where you want to use.

see this article if you don't know what provider is: Flutter StateManagement with Provider