Capture cloud firestore document snapshot error in Flutter

1,497

Solution 1

"Missing or insufficient permissions" means that your query violated one of your security rules. You will need to examine those rules, and make sure they allow the query you intend to perform.

There is plenty of documentation for security rules, and it's necessary to understand how they work in order to work with Firestore effectively from web and mobile clients.

It's not true that you can't catch an error from a Firestore query. You can't use try/catch - you will have to pass an error handler to listen().

Solution 2

I was having the same issue. PERMISSION_DENIED was coming out in the logs but I wanted to catch the error myself so that I could display it to the user. I found this issue on GitHub:

Firebase - native error messages not provided issue

It states that a lot of work has been done to improve the error handling in Firebase. So I spent yesterday upgrading my app to the latest version of firebase_auth (0.18.0 at the time of writing) and I can now catch and handle the PERMISSION_DENIED error like this:

return StreamBuilder<List<DistanceDocSnapshot>>(
    stream: _eventStream,
    builder: (BuildContext context,
        AsyncSnapshot<List<DistanceDocSnapshot>> snapshot) {
      if (snapshot.hasError) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            'Error retrieving events: ${snapshot.error.toString()}',
            style: TextStyle(fontSize: 20.0),
            textAlign: TextAlign.center,
          ),
        );
      }
      if (snapshot.hasData) {
         // Handle data as desired
      }
    }
  );

This can be seen working in the following screenshot Screenshot of error on my app (I had to provide a link to the screenshot because I don't have enough rep to embed images yet)

My code is laid out differently to yours but I think yours will start working as desired if you just upgrade your firebase_auth version.

Share:
1,497
Shashi Kiran
Author by

Shashi Kiran

Updated on December 23, 2022

Comments

  • Shashi Kiran
    Shashi Kiran over 1 year

    I am trying to capture the error thrown by firstore plugin in flutter when listening to document snapshots. The error is thrown in the debug logs but I cannot access it on catch error or handle error. Is this an enhancement needed for the plugin or is there a way?

    Error in debug

    I/System.out(16041): com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

    Here is my code, I tried a number of ways but it didn't work

    _getUserCollection.document(uid).snapshots();
    
    _getUserCollection.document(uid).snapshots().handleError((onError) {
          print(onError.toString());
        });
    
     try {
          _getUserCollection.document(uid).snapshots();
        } catch (e) {
          print(e);
        }
    
     try {
          _getUserCollection.document(uid).snapshots();
        } on PlatformException catch (e) {
          print(e.toString());
        }
    
     _getUserCollection.document(uid).snapshots().listen((event) {
          print('here on  listen');
        }, onError: (e) {
          print('on error $e');
        });
    
  • Shashi Kiran
    Shashi Kiran over 3 years
    Hi Doug, Thank you for your answer. I am using the idtoken expiry time in the security rules to not allow expired id tokens to query the database. I have used the onError handle for listen in the number 4 code. Is that the right approach?
  • Doug Stevenson
    Doug Stevenson over 3 years
    You don't have to write any rules to check expiration. Expired tokens are rejected automatically. Are you sure that the error printed to the log isn't actually your error handler output?
  • Shashi Kiran
    Shashi Kiran over 3 years
    Hi Doug, Yes the error is not being caught for snapshot but when you use get it is caught.
  • Doug Stevenson
    Doug Stevenson over 3 years
    If you have a specific question about your rules, please post a new question that shows the rules along with the queries that do not behave the way you expect.