skip to Main Content

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,
      })

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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,
                      ),
                    ),
                  ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search