Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter query multiple collections in firestore

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;
  }
like image 709
JWRich Avatar asked Aug 01 '18 22:08

JWRich


People also ask

How do I query multiple values in firestore?

Starting with… in queries! With the in query, you can query a specific field for multiple values (up to 10) in a single query. You do this by passing a list containing all the values you want to search for, and Cloud Firestore will match any document whose field equals one of those values.

How do I check if a collection exists in firestore flutter?

So to know if a Collection exists of no, you can run a query that checks for a field in Info Documents (eg CollectionInfo. exists) to know which Collections have been already created. However, please keep in mind that this will have an extra cost every extra Read/Write operation.

What is a Subcollection in firestore?

A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms. class roomA.


1 Answers

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);
      });
    }
  }
});
like image 124
JWRich Avatar answered Sep 20 '22 17:09

JWRich