简体   繁体   中英

How to catch platformException in flutter/dart?

I am getting

Exception has occurred.

PlatformException (PlatformException(sign_in_canceled,
com.google.android.gms.common.api.ApiException: 12501: , null))

whenever I try to cancel login with google pop up in my app.

STEP TO REPRODUCE:

In the app when pressed login with google button the google sign in page appears with all the gmail accounts to choose from to login or signup.

Touch the outer area of the pop up to cancel the sign in, which then shuts down the pop up.

After that there is platform exception in the code saying:

Exception has occurred.
PlatformException (PlatformException(sign_in_canceled,
com.google.android.gms.common.api.ApiException: 12501: , null))

Now the login button is not working and the app crashes,

Tried try/catch - not working

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
}

PlatformException: Thrown to indicate that a platform interaction failed in the platform plugin. (null safety)

I was facing a similar issue. Adding the services import solved it. Check the docs for more information.

import 'package:flutter/services.dart';

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!

Apparently, this issue is still not fixed and it does not occur if you run the flutter app from the terminal using flutter run -d Don't worry about the release version of your application. It does not crashes or freezes in the release version.

This is how you can catch platform exception

try {
  //
} on PlatformException catch (e) {
  if (e.code == '???') {
     // 
  }
} catch (e) {
  //
}

I have the same issue with SignIn with google. User cancel operation or internet problem. I have Searched a lot and tried a lot of Exception catch solutions. but in the end, I found something Helpful and decide to share it here.

Just try to run your project without debugging mode .

use this it works for me

signInWithGoogle(BuildContext context) async {
        try {
          final GoogleSignInAccount? googleUser =
          await GoogleSignIn(scopes: <String>['email']).signIn();
          if (googleUser != null) {
            final GoogleSignInAuthentication googleSignInAuthentication =
            await googleUser.authentication;
            final credential = GoogleAuthProvider.credential(
                accessToken: googleSignInAuthentication.accessToken,
                idToken: googleSignInAuthentication.idToken);
            try{
              await FirebaseAuth.instance
                .signInWithCredential(credential);
            } on FirebaseAuthException catch(e){
              print("********${e.message}*********");
            }
          }
        } on PlatformException catch (e) {
          print("********${e.message}*********");
        }
      }

output: ********com.google.android.gms.common.api.ApiException: 7: *********

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM