Cloud Firestore exception handling

0 votes

My flutter app is using firestore. In the firestore collection 'members' I am restricting adding new documents having the same mobileNo using a security rule

match /members/{mobileNo} {
      allow create: if !exists(/databases/$(database)/documents/members/$(mobileNo))
    }

I am putting the dart code in a try catch block like so

try {
    await members.doc().set({
                'id': members.doc().id,
                'name': name,
                'mobileNo': mobileNo,
                'address': address,

              });
    } catch (e) {
MyMessageHandler.showSnackBar(_scaffoldKey, 'number already registered!');
    }

so I can catch the exception and provide an error message. Instead of the generic catch(e), I want to catch a specific exception using on SpecificFireStoreException catch(e) clause and deal with it. But I don't know the type of exceptions thrown by the security rules by firestore so as to code like the FirebaseAuthException

}on FirebaseAuthException catch(e) {
 if (e.code = 'weak-password'){
MyMessageHandler.showSnackBar(_scaffoldKey, 'Weak password!!');
}

I want to do something similar but with the exception given by the security rule. Thanks for the help.

Apr 6, 2023 in Flutter by Ashwini
• 5,430 points
1,933 views

1 answer to this question.

0 votes

Cloud Firestore throws a variety of exceptions depending on the operation and the specific error encountered. However, you can catch exceptions related to security rules using the FirebaseFirestoreException class.

The FirebaseFirestoreException class has the following properties:

  • code: a string that represents the error code.
  • message: a string that describes the error message.
  • details: a list of strings that contains additional details about the error.

Here's an example of how you can catch FirebaseFirestoreException in your code:

try {
  await members.doc().set({
    'id': members.doc().id,
    'name': name,
    'mobileNo': mobileNo,
    'address': address,
  });
} on FirebaseFirestoreException catch (e) {
  if (e.code == 'permission-denied') {
    MyMessageHandler.showSnackBar(_scaffoldKey, 'You do not have permission to perform this action.');
  } else if (e.code == 'already-exists') {
    MyMessageHandler.showSnackBar(_scaffoldKey, 'Number already registered!');
  } else {
    MyMessageHandler.showSnackBar(_scaffoldKey, 'An error occurred: ${e.message}');
  }
}

In this example, you can catch FirebaseFirestoreException using the on keyword and the catch clause. You can then access the code property of the exception to determine the type of error and provide an appropriate error message.

Note that the specific error codes that Firestore throws can change over time, so you should consult the Firestore documentation to ensure that you are using the correct codes for your use case.

answered Apr 6, 2023 by veena

Related Questions In Flutter

0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
3,443 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,008 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,054 views
0 votes
1 answer

How to create a function in Dart language?

Hi@akhtar, There are many function types available in ...READ MORE

answered Jul 22, 2020 in Others by MD
• 95,460 points
700 views
0 votes
1 answer

Xcode Cloud not building my Flutter Project

It's difficult to determine the exact cause ...READ MORE

answered Apr 11, 2023 in Flutter by Anitha
1,698 views
0 votes
1 answer

Exception Handling: “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it

Iterator<String> iter = myArrayList.iterator(); while (iter.hasNext()) { ...READ MORE

answered Jan 10, 2019 in Java by Sushmita
• 6,920 points
705 views
0 votes
0 answers

Exception handling in UiPath

Can anyone tell me what are the ...READ MORE

Mar 6, 2019 in RPA by Misha
474 views
0 votes
1 answer

How exception handling can be done in Selenium?

@Suresh, Selenium exceptions can be handled in ...READ MORE

answered May 23, 2019 in Selenium by Suman
670 views
0 votes
1 answer

Oozie exception error handling.

Hi, We can set Hive Action configuration using ...READ MORE

answered Jun 18, 2019 in Big Data Hadoop by Gitika
• 65,890 points
1,329 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP