Catching PlatformException in async function in flutter

549

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();
}
Share:
549
Ryan
Author by

Ryan

Updated on December 22, 2022

Comments

  • Ryan
    Ryan 1 minute

    Background

    I am trying to fetch a booking document from Firestore inside the initState() method of my State. 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, an async function that returns a Future<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 the get() call.

    Any help would be appreciated!

    • Christopher Moore
      Christopher Moore over 2 years
      Please copy-paste errors directly into the question with the formatting tools that are provided.
    • Christopher Moore
      Christopher Moore over 2 years
      Also addressing your thought of you retrieving the Future in initState being a mistake, this is actually the intended method retrieving a Future for FutureBuilder(assuming you're using it to display what you retrieve) according to the docs. Many people don't do this, instead retrieving the Future in build without really understanding the consequences. There may be some cases where this is intended of course.
  • Ryan
    Ryan over 2 years
    Thanks 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 the try / catch altogether and allow the exception to set the snapshots hasError method to true. 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.