Dart - FirebaseFirestore exception handling

Viewed 629

The documentation is very poor about exception handling when using firebase firestore. I'm about to assume there is no such thing to handle specific errors.

I'm currently using the latest dependencies:

  • firebase_core: ^0.5.0
  • firebase_auth: ^0.18.0+1
  • cloud_firestore: ^0.14.0+2

On firebase_auth it is well documented and straight forward how to handle exceptions. There is an exception class named FirebaseAuthException which has a code field that contains the specific error code (unfortunately as a String, but at least it provides something) such as invalid-email, user-disabled, weak-password and so on. It is a nice to have because I can notify the user what went wrong and the program can act accordingly based on these error codes.

On the other hand, unfortunately there is no such thing as FirebaseFirestoreException and the documentation is not much of a help. It shows nothing more than a .catchError() to handle possible exceptions with an error variable with dynamic type so it basically can be anything.

Future<void> addUser() {
  // Call the user's CollectionReference to add a new user
  return users
      .add({
        'full_name': fullName,
        'company': company,
        'age': age
      })
      .then((value) => print("User Added"))
      .catchError((error) => print("Failed to add user: $error"));
}

So my question is if there anything or similar to handle specific exception cases like in FirebaseAuthException with some error codes and stuff. And furthermore how can I identify if it is a FirebaseFirestore exception? Why it does not provide exceptions to make things more comfortable for developers? Or am I missing the point? Please let me know.

Documentation about firebase firestore with flutter: https://firebase.flutter.dev/docs/firestore/usage

0 Answers
Related