How to catch platformException in flutter/dart?

try {
  final res = await _auth.signInWithEmailAndPassword(
      email: email, password: password);
  if (res != null) loggedUser = res.user;
} on PlatformException catch (err) {
  // Handle err
} catch (err) {
  // other types of Exceptions
}

I have also recently faced this error and, I have found out that the .catchError() callback wasn't being called in the debug mode (which is when you click the Run->Start Debugging button in VSCode).

However, when you type in flutter run -d , then, the .catchError() method gets called back as it is not in debug mode.

To get your preferred simulator's code paste this line of code in the terminal:

instruments -s devices

If that doesn't work, you can also try pasting this:

xcrun simctl list

The .catchError() method will get called unlike before and the code inside that will get executed as expected!

Additionally, the app won't crash anymore with a PlatformException() and instead you will get a log like this one:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null

I have been facing this problem in Google Sign In too, in which the .catchError() was not being called!

In conclusion, if you have some error with handling errors in Firebase Authentication, you should first try to first run in through the terminal. Thanks, and I hope this helps and trust me, it should work for you!