gpt4 book ai didi

flutter - Firestore-查询在哪里映射键

转载 作者:行者123 更新时间:2023-12-03 03:30:46 25 4
gpt4 key购买 nike

我正在做一个条形码扫描应用程序,如果要扫描它,它会在订单中更新项目的状态。我已经有3个查询了,我知道它开始出错了。
我的最终目标应该是这样的:Barcode scan -> query current order number -> query item number (if included in the order) -> update消防站内容:
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());
});

最佳答案

问题解决了。在跳转到第二个查询之前,我已经添加了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());
});

关于flutter - Firestore-查询在哪里映射键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64012562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com