FadyFouad
FadyFouad

Reputation: 948

Wait till data in firestore has successfully updated

I want to check if data is uploaded successfully to firestore when the user click the save button and show toast or ask the user to try again if the document not update.

this is my code to update the document.

  Future updateTechnical(Technical technical) async {
    return await technicalsCollection.doc(technical.id).update({
      "name": technical.name,
      "address": technical.address,
      "phoneNum": technical.phoneNum,
      "tools": technical.tools,
    });
  }

And this is the save Button

       ElevatedButton(
                onPressed: () {
                  DatabaseService().updateTechnical(
                    Technical(
                      id: selectedTechnical.id,
                      name: nameController.text,
                      address: addressController.text,
                      phoneNum: phoneController.text,
                      tools: selected.join(','),
                      
                    ),
                  );
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('Save  '),
                    Icon(Icons.person_add),
                  ],
                ),
              ),

Upvotes: 3

Views: 995

Answers (2)

Paul
Paul

Reputation: 1875

To wait till the data has been successfully updated you can use .then().

onPressed: () {
   DatabaseService().updateTechnical().then((value) => {
                         print("successfully uploaded!"),
                    });
},

To check if any errors have occurred during upload you should check in updateTechnical():

 Future updateTechnical(Technical technical) async {
    try {
      final querySnapshot =
          await technicalsCollection.doc(technical.id).update({
        "name": technical.name,
        "address": technical.address,
        "phoneNum": technical.phoneNum,
        "tools": technical.tools,
      });
      return querySnapshot;
    } catch (error) {
      print(error); //here you can see the error messages from firebase
      throw 'failed';
    }
  }

If you need to do certain things, when a specific error occurs, you, unfortunately, have to check through all the error messages. Here is an example: Handle exceptions

Upvotes: 1

Hydra
Hydra

Reputation: 1000

To check if your data updated successfully, you'll want to catch any errors if they occur. I've modified your updateTechnical() function to do so here:

  Future updateTechnical(Technical technical) async {
    return await technicalsCollection.doc(technical.id).update({
      "name": technical.name,
      "address": technical.address,
      "phoneNum": technical.phoneNum,
      "tools": technical.tools,
    }).then((value) => print("Updated successfully")) // success toast here
          .catchError((error) => print("Failed to update: $error")); // fail toast here
  }

You could also use a try/catch block, but that is considered a little less efficient and is generally avoided where possible

Upvotes: 1

Related Questions