我想在Flutter中通过文档值更新Firestore中的数据。

huangapple go评论61阅读模式
英文:

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(&#39;builderData&#39;)
.where(&#39;Property Id&#39;, isEqualTo: propertyId)
.get();
final querySnapshotAdv = await firestore
.collection(&#39;adviserData&#39;)
.where(&#39;Property Id&#39;, 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(&#39;adviserData&#39;);
CollectionReference buildercollRef =
FirebaseFirestore.instance.collection(&#39;builderData&#39;);
final adviserDeleteFuture =
advisercollRef.doc(adviserdocumentId).update({
&#39;Property Title&#39;: propertyTitleController.text,
&#39;Property Address&#39;: propertyAddressController.text,
&#39;Property Cover&#39;: propertyCoverController.text,
&#39;Property Description&#39;: propertyDescriptionController.text,
&#39;Google Maps Link&#39;: propertyLocationController.text,
&#39;Property Status&#39;: propertyStatusController.text,
&#39;Price Sqft&#39;: pricePerSqftController.text,
&#39;Posssesion Date&#39;: possessionDateController.text,
&#39;Max Sqft&#39;: maxAreaController.text,
&#39;Min Sqft&#39;: minAreaController.text,
&#39;Rera Id&#39;: rearaIdController.text,
&#39;Total Floors&#39;: totalFloorsController.text,
&#39;Total Units&#39;: totalUnitsController.text,
&#39;One Bhk&#39;: oneBhkConfigController.text,
&#39;Two Bhk&#39;: twoBhkConfigController.text,
&#39;Three Bhk&#39;: threeBhkConfigController.text,
&#39;Four Bhk&#39;: fourBhkConfigController.text,
&#39;Five Plus Bhk&#39;: fivePlusBhkConfigController.text,
&#39;Launch Date&#39;: launchDateController.text,
&#39;Total Project Area&#39;: totalProjectAreaController.text,
});
final builderDeleteFuture =
buildercollRef.doc(builderdocumentId).update({
&#39;Property Title&#39;: propertyTitleController.text,
&#39;Property Address&#39;: propertyAddressController.text,
&#39;Property Cover&#39;: propertyCoverController.text,
&#39;Property Description&#39;: propertyDescriptionController.text,
&#39;Google Maps Link&#39;: propertyLocationController.text,
&#39;Property Status&#39;: propertyStatusController.text,
&#39;Price Sqft&#39;: pricePerSqftController.text,
&#39;Posssesion Date&#39;: possessionDateController.text,
&#39;Max Sqft&#39;: maxAreaController.text,
&#39;Min Sqft&#39;: minAreaController.text,
&#39;Rera Id&#39;: rearaIdController.text,
&#39;Total Floors&#39;: totalFloorsController.text,
&#39;Total Units&#39;: totalUnitsController.text,
&#39;One Bhk&#39;: oneBhkConfigController.text,
&#39;Two Bhk&#39;: twoBhkConfigController.text,
&#39;Three Bhk&#39;: threeBhkConfigController.text,
&#39;Four Bhk&#39;: fourBhkConfigController.text,
&#39;Five Plus Bhk&#39;: fivePlusBhkConfigController.text,
&#39;Launch Date&#39;: launchDateController.text,
&#39;Total Project Area&#39;: totalProjectAreaController.text,
});
await Future.wait([adviserDeleteFuture, builderDeleteFuture]);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(&#39;Success&#39;),
content: Text(&#39;Property Updated Successfully&#39;),
actions: &lt;Widget&gt;[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(&#39;Ok&#39;),
),
],
);
},
);
},
padding: const EdgeInsets.symmetric(vertical: 15.0),
),
icon: Icon(Icons.arrow_circle_up),
label: Text(
&#39;Submit Changes&#39;,
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(&quot;builderData&quot;);
// Create a query against the collection.
final query = builderRef.where(&quot;Property Id&quot;, isEqualTo: widget.propertyId);
query.get().then(
(querySnapshot) {
print(&quot;Successfully completed&quot;);
for (var docSnapshot in querySnapshot.docs) {
docSnapshot.reference.update({
... your data here
});
}
},
onError: (e) =&gt; print(&quot;Error completing: $e&quot;),
);

And then you'll need to do the same for the other collection where you want to update a document.

huangapple
  • 本文由 发表于 2023年5月7日 21:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定