iamlux20
iamlux20

Reputation: 158

Firestore - Query where map key

I'm doing a barcode scan app where it updates the status of an item in an order if it gets scanned. I'm already 3 queries deep and I know it's starting to get wrong.

My end goal should be like this:

Barcode scan -> query current order number -> query item number (if included in the order) -> update

Firestore content:

Firestore content

        var result = await BarcodeScanner.scan();
        var find = result.rawContent;

        Firestore.instance
            .collection('srs-items')
            .document(docId)
            .get()
            .then((DocumentSnapshot documentSnapshot) {
            
                print("Val: " + documentSnapshot.reference.path);
                    message = "Item scanned";

                    // I got lost here

                   // This query should be where('9781452103068', isEqualTo: find)
                   // or whatever the dynamic way to call the key of the map


                    Firestore.instance
                        .collection('srs-items')
                        .where(documentSnapshot.reference.path + "/$find", isEqualTo: find)
                        .limit(1)
                        .getDocuments()
                        .then((QuerySnapshot querySnapshot) {
                            print(querySnapshot.documents.length.toString());
                            if(querySnapshot.documents.length == 0)
                                message = "Item not in SRS listings";
                            else {
                                message = "Item scanned";

                                // not sure if there's a need for 3rd query for updating 9781452103068's status

                                /*Firestore.instance
                                    .collection('srs-items')
                                    .document(docId)
                                    .updateData({
                                    "$find.status": "2"
                                });*/
                            }
                            print(message);
                            _showToast(context, message);
                        });
               
                
            })
            .catchError((onError) {
                print(onError.toString());
            });

Upvotes: 1

Views: 244

Answers (1)

iamlux20
iamlux20

Reputation: 158

Problem solved. I've added if(key == find) {} before jumping into the 2nd query. So the final code looks like this:

 var result = await BarcodeScanner.scan();
        var find = result.rawContent;
        String message = "";
         Firestore.instance
            .collection('srs-items')
            .document(docId)
            .get()
            .then((DocumentSnapshot documentSnapshot) {
                documentSnapshot.data.forEach((key, value) {

                    if(key == find) { // <--- added this

                        message = "Item scanned";
                         Firestore.instance
                            .collection('srs-items')
                            .where( FieldPath.documentId , isEqualTo: find)
                            .limit(1)
                            .getDocuments()
                            .then((QuerySnapshot querySnapshot) {
                            Firestore.instance
                                .collection('srs-items')
                                .document(docId)
                                .updateData({
                                "$key.status": "2"
                            });
                        })
                        .catchError((onError) {
                            print("Err: " + onError.toString());
                        });
                    }
                    else {
                        message = "Item not in SRS listings";
                    }
                });
            })
            .catchError((onError) {
                print("Err: " + onError.toString());
            });

Upvotes: 1

Related Questions