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');
});
"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().
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With