flutter query multiple collections in firestore

12,517

Short answer i didn't pay attention to how you use a future

Get the productId from the user table

  Future<List<DocumentSnapshot>> getProduceID() async{
    var data = await Firestore.instance.collection('users').document(widget.userId).collection('Products').getDocuments();
    var productId = data.documents;
    return productId;
  }

Then call this using .then() rather than try to apply the returned data to the variable as that is not how a future it works

  var products;
  getProduceID().then((data){
  for(int i = 0; i < s.length; i++) {
    products = Firestore.instance.collection('products')
        .document(data[i]['productID'])
        .snapshots();
    if (products != null) {
      products.forEach((product) {
        print(product.data.values);
      });
    }
  }
});
Share:
12,517

Related videos on Youtube

JWRich
Author by

JWRich

I am a developer!

Updated on September 15, 2022

Comments

  • JWRich
    JWRich over 1 year

    I am playing with flutter but I ran into an issue with firestore that I can't figure out.

    let's say I would like to retrieve the purchaser history of a customer and I have a firestore as described below so I have a collection of "user" within that contains a document of user_id's and then within that, I have a "products" collection with a document that contains the product_id's my aim is to gather the product id's for the selected user and then retrieve the products price from the products collection?

    • users

      • user_id_1
        • products
          • purchace_id
            • product_id
            • quantity
    • products

      • product_id
        • product_desc
        • price

    the issue I am having is while I can get the id for the products from the user table but I can't then see a simple way of how I can use this data like with SQL where we would make a simple join on the product_id to get the price where the id's match?

    any help would be much appreciated.


    Thanks for replying Frank van Puffelen 2 seprate querys seams reasonable but it brings up other issues with the second query as now with the code below i can get the documents_id from the user and then do a query on the products and everything looks ok so i think i am going in the correct direction as i can print the id of the document in the loop to ensure i am accessing the correct ones but when i change this to get a snapshot of the document i cant access the data within this is probably me doing something silly or misunderstanding something but this feels very awkward compared to sql when trying to get and work with the data. For example if i return the data it wants it to be in the form of a future> however once i return the data in that format then i can no longer access the id's in the same way as i was doing.

    Future<List<DocumentSnapshot>> getSeedID() async{
        var data = await Firestore.instance.collection('users').document(widget.userId).collection('products').getDocuments();
        var productList = data.documents;
        print("Data length: ${productList.length}");
        for(int i = 0; i < productList.length; i++){
          var productId = Firestore.instance.collection('products').document(productList[i]['productId']).documentID;
          if(productId != null) {
            print("Data: " + productId);
          }
        }
        return productList;
      }
    
  • somnath
    somnath over 4 years
    This works great for Futures but doesnt seem to be working with streams. I am trying to get similar detail data within a function inside the the ListView.builder itembuilder() as I cant find a way to do a .then in the stream() as it returns snapshots instead?! Do you have any workarounds or ideas for that? TIA!
  • sean.boyer
    sean.boyer almost 4 years
    You can nest the ListBuilder inside of a StreamBuilder, so the ListBuilder will have the snapshot data. Test for it with snapshot.hasData
  • Noobdeveloper
    Noobdeveloper almost 3 years
    Can you please check out my question if possible stackoverflow.com/questions/68351097/…