首页 > 解决方案 > Firestore - 查询地图键的位置

问题描述

我正在做一个条形码扫描应用程序,如果它被扫描,它会更新订单中项目的状态。我已经深入了 3 个查询,我知道它开始出错了。

我的最终目标应该是这样的:

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

Firestore 内容:

Firestore 内容

        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());
            });

标签: flutterdart

解决方案


问题解决了。我if(key == find) {} 在进入第二个查询之前添加了。所以最终的代码是这样的:

 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());
            });

推荐阅读