Naveed Mir
Naveed Mir

Reputation: 31

I/flutter (18793): PlatformException(network_error, com.google.android.gms.common.api.ApiException: 7: , null, null)

I'm working on authentication part of my flutter project. I want to carryout out exception handling for login when the phone is not connected to internet. The app has 3 auth providers: Phone, GoogleSignIn, and email. The error message is displayed properly for phone signin and email sigin using FirebaseException, which displays the error message. But for GoogleSignIn, if the device is not connected to the internet the code fails at :

final GoogleSignInAccount? googleUser = await googleSignIn.signIn();

Since the execution fails at this point I think FirebaseAuthException doesnt work as it is not supported in googleSignIn.

For which FirebaseAuthException doesnt work, but only "Exception" works. And i dont get the proper error message using Exception in an alert Dialog.

Code for Email SignIn with exception handling.

Future<void> signInWithEmailAndPassword(
      String email,
      String password,
      void Function(FirebaseAuthException e) errorCallback,
      ) async {
    try {
      await FirebaseAuth.instance.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
    } on FirebaseAuthException catch (e) {
      errorCallback(e);
    }
  }

Error Dialog which gets called in errorCallback(e)

void _showErrorDialog(BuildContext context, String title, Exception e) {
    showDialog<void>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text(
            title,
            style: const TextStyle(fontSize: 24),
          ),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Text(
                  '${(e as dynamic).message}',
                  style: const TextStyle(fontSize: 18),
                ),
              ],
            ),
          ),
          actions: <Widget>[
            ElevatedButton(onPressed: () {
              Navigator.of(context).pop();
              },
              child: const Text('OK',
              style: TextStyle(color: Colors.deepPurple),
            ),)
          ],
        );
      },
    );
  }

Exception handling using FirebaseAuthException for email

Exception handling using FirebaseAuthException for email


Google SignIn

Future<void> signInWithGoogle (
      void Function(Exception e) errorCallback,
      ) async {
    try{
      final GoogleSignInAccount? googleUser = await googleSignIn.signIn();
      final GoogleSignInAuthentication? googleAuth = await googleUser!.authentication;
      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth!.accessToken,
        idToken: googleAuth.idToken,
      );
      await FirebaseAuth.instance.signInWithCredential(credential);
    }
    on Exception catch (e) {
      print(e);
      errorCallback(e);
    }
  }

Exception handling for google login using Exception instead of FirebaseAuthException

Error printed in console I/flutter ( 4383): PlatformException(network_error,com.google.android.gms.common.api.ApiException: 7: , null, null)

P.S I just want the proper network error to be displayed in the alert dialog and not " com.google.android.gms.common.api.ApiException: 7"

Upvotes: 1

Views: 5021

Answers (3)

Adeel Nazim
Adeel Nazim

Reputation: 724

I update your code now it's working properly also make sure you have an an active Internet Connection

import 'package:google_sign_in/google_sign_in.dart';

Future<UserCredential> signInWithGoogle() async {
  // Trigger the authentication flow
  final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

  // Obtain the auth details from the request
  final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;

  // Create a new credential
  final credential = GoogleAuthProvider.credential(
    accessToken: googleAuth?.accessToken,
    idToken: googleAuth?.idToken,
  );

  // Once signed in, return the UserCredential
  return await FirebaseAuth.instance.signInWithCredential(credential);
}

error: corn.google.android.gms.common.api .ApiException: 7:

This error occurs when you are not connect to the internet.

Upvotes: 0

Priyanka Makwana
Priyanka Makwana

Reputation: 11

Error: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(network_error, com.google.android.gms.common.api.ApiException: 7: , null, null)

Solution: Make sure you have an active Internet Connection, (Network error specifies that you are not connected to the Internet or you don't have a stable network connection)

Upvotes: 0

Naveed Mir
Naveed Mir

Reputation: 31

Update

Since FirebaseAuthException isn't called as execution fails at GoogleSignIn, FirebaseAuthException cannot be used.

While the error printed in log is a PlatformException, we can use PlatformException instead of Exception in the try catch block and check the error code since its supported by PlatformException.

The error code can be compared with GoogleSignIn.kNetworkError and GoogleSignIn.kSignInFailedError.

Future<void> signInWithGoogle (
    void Function(String errorMessage) errorCallback,
) async {
    try {
        final GoogleSignInAccount? googleUser = await googleSignIn.signIn();
        final GoogleSignInAuthentication? googleAuth = await googleUser!.authentication;
        final credential = GoogleAuthProvider.credential(
            accessToken: googleAuth!.accessToken,
            idToken: googleAuth.idToken,
        );
        await FirebaseAuth.instance.signInWithCredential(credential);
    }
    on PlatformException catch (e) {
        if (e.code == GoogleSignIn.kNetworkError) {
            String errorMessage = "A network error (such as timeout, interrupted connection or unreachable host) has occurred.";
            errorCallback(errorMessage);
        } else {
            String errorMessage = "Something went wrong.";
            errorCallback(errorMessage);
        }
    }
}

Screenshot

Upvotes: 2

Related Questions