英文:
I want to update data in firestore by the document value in flutter
问题
我有两个Firebase集合,一个是builderData,另一个是adviserData,两者都包含名为'Property Id'的字段。我想在这两个集合中更新Property Id相等的属性的值。
在按下此按钮时,我希望根据adviserData和builderData集合中的Property Id更新属性。
ElevatedButton.icon(
onPressed: () {
CollectionReference buildercollRef =
FirebaseFirestore.instance.collection('builderData');
CollectionReference adviserdocRef =
FirebaseFirestore.instance.collection('adviserData');
buildercollRef.doc(widget.propertyId).update({
'Property Title': propertyTitleController.text,
'Property Address': propertyAddressController.text,
'Property Cover': propertyCoverController.text,
'Property Description': propertyDescriptionController.text,
'Google Maps Link': propertyLocationController.text,
'Property Status': propertyStatusController.text,
'Price Sqft': pricePerSqftController.text,
'Posssesion Date': possessionDateController.text,
'Max Sqft': maxAreaController.text,
'Min Sqft': minAreaController.text,
'Rera Id': rearaIdController.text,
'Total Floors': totalFloorsController.text,
'Total Units': totalUnitsController.text,
'One Bhk': oneBhkConfigController.text,
'Two Bhk': twoBhkConfigController.text,
'Three Bhk': threeBhkConfigController.text,
'Four Bhk': fourBhkConfigController.text,
'Five Plus Bhk': fivePlusBhkConfigController.text,
'Launch Date': launchDateController.text,
'Total Project Area': totalProjectAreaController.text,
});
}
)
英文:
I have two firebase collections one is builderData and another is adviserData, both contains a filed named 'Property Id'. I want to update the value of a property in both the collections where the Property Id's are equal in both collections.
On Pressing of this button I want to update the property based on the Property Id in both the collections adviserData and builderData.
ElevatedButton.icon(
onPressed: () {
CollectionReference buildercollRef =
FirebaseFirestore.instance.collection('builderData');
CollectionReference adviserdocRef =
FirebaseFirestore.instance.collection('adviserData');
buildercollRef.doc(widget.propertyId).update({
'Property Title': propertyTitleController.text,
'Property Address': propertyAddressController.text,
'Property Cover': propertyCoverController.text,
'Property Description': propertyDescriptionController.text,
'Google Maps Link': propertyLocationController.text,
'Property Status': propertyStatusController.text,
'Price Sqft': pricePerSqftController.text,
'Posssesion Date': possessionDateController.text,
'Max Sqft': maxAreaController.text,
'Min Sqft': minAreaController.text,
'Rera Id': rearaIdController.text,
'Total Floors': totalFloorsController.text,
'Total Units': totalUnitsController.text,
'One Bhk': oneBhkConfigController.text,
'Two Bhk': twoBhkConfigController.text,
'Three Bhk': threeBhkConfigController.text,
'Four Bhk': fourBhkConfigController.text,
'Five Plus Bhk': fivePlusBhkConfigController.text,
'Launch Date': launchDateController.text,
'Total Project Area': totalProjectAreaController.text,
})
答案1
得分: 1
以下是代码部分的翻译:
首先,我们可以检查文档并获取具有与传递的属性 Id 相等的属性 Id 的文档,然后我们将获取所有这些文档的文档快照,稍后可以获取第一个文档的文档 Id 并执行更新操作。希望下面的代码能帮助您。
ElevatedButton.icon(
onPressed: () async {
final firestore = FirebaseFirestore.instance;
final propertyId = widget.propertyId;
final querySnapshot = await firestore
.collection('builderData')
.where('Property Id', isEqualTo: propertyId)
.get();
final querySnapshotAdv = await firestore
.collection('adviserData')
.where('Property Id', isEqualTo: propertyId)
.get();
final documentSnapshotAdv = querySnapshotAdv.docs.first;
final adviserdocumentId = documentSnapshotAdv.id;
final documentSnapshot = querySnapshot.docs.first;
final builderdocumentId = documentSnapshot.id;
CollectionReference advisercollRef =
FirebaseFirestore.instance.collection('adviserData');
CollectionReference buildercollRef =
FirebaseFirestore.instance.collection('builderData');
final adviserDeleteFuture =
advisercollRef.doc(adviserdocumentId).update({
'Property Title': propertyTitleController.text,
'Property Address': propertyAddressController.text,
'Property Cover': propertyCoverController.text,
'Property Description': propertyDescriptionController.text,
'Google Maps Link': propertyLocationController.text,
'Property Status': propertyStatusController.text,
'Price Sqft': pricePerSqftController.text,
'Posssesion Date': possessionDateController.text,
'Max Sqft': maxAreaController.text,
'Min Sqft': minAreaController.text,
'Rera Id': rearaIdController.text,
'Total Floors': totalFloorsController.text,
'Total Units': totalUnitsController.text,
'One Bhk': oneBhkConfigController.text,
'Two Bhk': twoBhkConfigController.text,
'Three Bhk': threeBhkConfigController.text,
'Four Bhk': fourBhkConfigController.text,
'Five Plus Bhk': fivePlusBhkConfigController.text,
'Launch Date': launchDateController.text,
'Total Project Area': totalProjectAreaController.text,
});
final builderDeleteFuture =
buildercollRef.doc(builderdocumentId).update({
'Property Title': propertyTitleController.text,
'Property Address': propertyAddressController.text,
'Property Cover': propertyCoverController.text,
'Property Description': propertyDescriptionController.text,
'Google Maps Link': propertyLocationController.text,
'Property Status': propertyStatusController.text,
'Price Sqft': pricePerSqftController.text,
'Posssesion Date': possessionDateController.text,
'Max Sqft': maxAreaController.text,
'Min Sqft': minAreaController.text,
'Rera Id': rearaIdController.text,
'Total Floors': totalFloorsController.text,
'Total Units': totalUnitsController.text,
'One Bhk': oneBhkConfigController.text,
'Two Bhk': twoBhkConfigController.text,
'Three Bhk': threeBhkConfigController.text,
'Four Bhk': fourBhkConfigController.text,
'Five Plus Bhk': fivePlusBhkConfigController.text,
'Launch Date': launchDateController.text,
'Total Project Area': totalProjectAreaController.text,
});
await Future.wait([adviserDeleteFuture, builderDeleteFuture]);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Success'),
content: Text('Property Updated Successfully'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Ok'),
),
],
);
},
);
},
padding: const EdgeInsets.symmetric(vertical: 15.0),
),
icon: Icon(Icons.arrow_circle_up),
label: Text(
'Submit Changes',
style: GoogleFonts.montserrat(
fontSize: 13,
fontWeight: FontWeight.w500,
),
),
);
英文:
Here we can first check for the documents and get the documents which have the property Id equals to the property Id passed, then we will get the document snapshot of all those documents later we can get the document id of the first document and do the update operation. Hope below code will help you.
ElevatedButton.icon(
onPressed: () async {
final firestore = FirebaseFirestore.instance;
final propertyId = widget.propertyId;
final querySnapshot = await firestore
.collection('builderData')
.where('Property Id', isEqualTo: propertyId)
.get();
final querySnapshotAdv = await firestore
.collection('adviserData')
.where('Property Id', isEqualTo: propertyId)
.get();
final documentSnapshotAdv = querySnapshotAdv.docs.first;
final adviserdocumentId = documentSnapshotAdv.id;
final documentSnapshot = querySnapshot.docs.first;
final builderdocumentId = documentSnapshot.id;
CollectionReference advisercollRef =
FirebaseFirestore.instance.collection('adviserData');
CollectionReference buildercollRef =
FirebaseFirestore.instance.collection('builderData');
final adviserDeleteFuture =
advisercollRef.doc(adviserdocumentId).update({
'Property Title': propertyTitleController.text,
'Property Address': propertyAddressController.text,
'Property Cover': propertyCoverController.text,
'Property Description': propertyDescriptionController.text,
'Google Maps Link': propertyLocationController.text,
'Property Status': propertyStatusController.text,
'Price Sqft': pricePerSqftController.text,
'Posssesion Date': possessionDateController.text,
'Max Sqft': maxAreaController.text,
'Min Sqft': minAreaController.text,
'Rera Id': rearaIdController.text,
'Total Floors': totalFloorsController.text,
'Total Units': totalUnitsController.text,
'One Bhk': oneBhkConfigController.text,
'Two Bhk': twoBhkConfigController.text,
'Three Bhk': threeBhkConfigController.text,
'Four Bhk': fourBhkConfigController.text,
'Five Plus Bhk': fivePlusBhkConfigController.text,
'Launch Date': launchDateController.text,
'Total Project Area': totalProjectAreaController.text,
});
final builderDeleteFuture =
buildercollRef.doc(builderdocumentId).update({
'Property Title': propertyTitleController.text,
'Property Address': propertyAddressController.text,
'Property Cover': propertyCoverController.text,
'Property Description': propertyDescriptionController.text,
'Google Maps Link': propertyLocationController.text,
'Property Status': propertyStatusController.text,
'Price Sqft': pricePerSqftController.text,
'Posssesion Date': possessionDateController.text,
'Max Sqft': maxAreaController.text,
'Min Sqft': minAreaController.text,
'Rera Id': rearaIdController.text,
'Total Floors': totalFloorsController.text,
'Total Units': totalUnitsController.text,
'One Bhk': oneBhkConfigController.text,
'Two Bhk': twoBhkConfigController.text,
'Three Bhk': threeBhkConfigController.text,
'Four Bhk': fourBhkConfigController.text,
'Five Plus Bhk': fivePlusBhkConfigController.text,
'Launch Date': launchDateController.text,
'Total Project Area': totalProjectAreaController.text,
});
await Future.wait([adviserDeleteFuture, builderDeleteFuture]);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Success'),
content: Text('Property Updated Successfully'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Ok'),
),
],
);
},
);
},
padding: const EdgeInsets.symmetric(vertical: 15.0),
),
icon: Icon(Icons.arrow_circle_up),
label: Text(
'Submit Changes',
style: GoogleFonts.montserrat(
fontSize: 13,
fontWeight: FontWeight.w500,
),
),
),
答案2
得分: 0
以下是翻译好的部分:
"看起来你需要在每个集合上使用查询来查找与属性ID匹配的文档。例如,对于builderData
,可以像这样做:
// 创建到建造者集合的引用
final builderRef = db.collection("builderData");
// 对集合创建查询。
final query = builderRef.where("Property Id", isEqualTo: widget.propertyId);
query.get().then(
(querySnapshot) {
print("成功完成");
for (var docSnapshot in querySnapshot.docs) {
docSnapshot.reference.update({
... 在此添加您的数据
});
}
},
onError: (e) => print("完成时出错: $e"),
);
然后,您需要在要更新文档的其他集合上执行相同的操作。"
英文:
It sounds like you'll want to use a query on each collection to find the documents matching the property ID. For example, for the builderData
that could look like this:
// Create a reference to the builder collection
final builderRef = db.collection("builderData");
// Create a query against the collection.
final query = builderRef.where("Property Id", isEqualTo: widget.propertyId);
query.get().then(
(querySnapshot) {
print("Successfully completed");
for (var docSnapshot in querySnapshot.docs) {
docSnapshot.reference.update({
... your data here
});
}
},
onError: (e) => print("Error completing: $e"),
);
And then you'll need to do the same for the other collection where you want to update a document.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论