Catching PlatformException in async function in flutter
You can't catch errors with try-catch
blocks when you're not using await
.
To catch errors in the way that you want to call this future function, use the catchError
method of the Future
class.
Ex.
_futureBooking = firestoreInstance
.collection('bookings')
.document('1Twgjq5YTe3Oa12wwbs1')
.get()
.catchError(
(err) {
print("error fetching from firestore: $err");
}
);
Alternatively, you could still use try-catch
and with await
, while still maintaining your functionality. Ex.
Future<DocumentSnapshot> wrapperFunction() async {
try {
return await firestoreInstance
.collection('bookings')
.document('1Twgjq5YTe3Oa12wwbs1')
.get();
} catch (err) {
print("error fetching from firestore: $err");
}
}
@override
void initState() {
super.initState();
_futureBooking = wrapperFunction();
}

Ryan
Updated on December 22, 2022Comments
-
Ryan 1 minute
Background
I am trying to fetch a booking document from
Firestore
inside theinitState()
method of myState
. This is also my first time using flutter/dart, so apologies if I am making a very rookie mistake.Problem
When I run the
get()
method, anasync
function that returns aFuture<DocumentSnapshot>
, an error is thrown. I am trying to catch and handle this error. The error is an 'insufficient permissions' error which I intend to get.My code
class _MyHomePageState extends State<MyHomePage> { final firestoreInstance = Firestore.instance; Future<DocumentSnapshot> _futureBooking; @override void initState() { super.initState(); try { _futureBooking = firestoreInstance .collection('bookings') .document('1Twgjq5YTe3Oa12wwbs1') .get(); } catch (err) { print("error fetching from firestore: $err"); } }
Results
The
catch
block is never entered, and the app always crashes when making theget()
call.Any help would be appreciated!
-
Christopher Moore over 2 yearsPlease copy-paste errors directly into the question with the formatting tools that are provided.
-
Christopher Moore over 2 yearsAlso addressing your thought of you retrieving the
Future
ininitState
being a mistake, this is actually the intended method retrieving aFuture
forFutureBuilder
(assuming you're using it to display what you retrieve) according to the docs. Many people don't do this, instead retrieving theFuture
inbuild
without really understanding the consequences. There may be some cases where this is intended of course.
-
-
Ryan over 2 yearsThanks for this, worked great. As it turns out, my app was not actually crashing, but simply breaking on exceptions.. silly me. Since I am using a
FutureBuilder
, I removed thetry / catch
altogether and allow the exception to set the snapshotshasError
method totrue
. But very useful to know how to do this nonetheless. Thanks again! -
gsm over 1 year(important)And please note that all these Catching Platform Exception is work only with 'Release Vestion', It will not work on debugging.
-
Not A Bot over 1 year@gsm That is incorrect. All exceptions can be caught regardless of the compilation mode. If this is something you personally experienced, it is due to a setting in your IDE to catch all exceptions whether they are caught or not. There was also a bug(may or may not be fixed) in VS code that prevented that setting from being disabled.