flutter - Firestore - 查询地图键的位置
问题描述
我正在做一个条形码扫描应用程序,如果它被扫描,它会更新订单中项目的状态。我已经深入了 3 个查询,我知道它开始出错了。
我的最终目标应该是这样的:
Barcode scan -> query current order number -> query item number (if included in the order) -> update
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());
});
解决方案
问题解决了。我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());
});
推荐阅读
- azure - Sharepoint 在线“不支持的仅应用程序令牌。”
- c++ - CodeLite 立即关闭终端
- flutter - Flutter:长文本链接,如隐私政策和服务条款
- mysql - 使用多个连接构建复杂的 MySQL 查询
- ssrs-2012 - 当项目编号发生变化时插入显示总数的空行
- glsl - 在顶点着色器中应用 Y 旋转时纹理坐标移动
- database - 调整 Oracle 数据库以加快启动(闪回)
- django - 使用 AJAX 时无法获取 POST 或 GET 表单数据
- windows - 如何使用非管理员用户从 Powershell 脚本自动将计算机添加到域
- sql-server - 检查sql语句是否返回ssis中的空记录集