android – Flutter, app crashes when I try to sign in with google


I had no problem building this on android but when I tried building on iOS I encountered this error. I was able to do all the authentication process on Android with no problem but I cant on iOS.

Full code and error are below :

social_buttons.dart

class NSocialButtons extends StatelessWidget {
  const NSocialButtons({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(LoginController());

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          decoration: BoxDecoration(
              border: Border.all(color: NColors.grey),
              borderRadius: BorderRadius.circular(100)),
          child: IconButton(
              onPressed: () => controller.googleSignIn(),
              icon: const Image(
                width: NSizes.iconLg,
                height: NSizes.iconLg,
                image: AssetImage(NImages.google),
              )),
        )
      ],
    );
  }
}

login_controller.dart

class LoginController extends GetxController {
  static LoginController get instance => Get.find();

  // variables
  final email = TextEditingController();
  final password = TextEditingController();
  final localStorage = GetStorage();
  final hidePassword = true.obs;
  final rememberMe = false.obs;
  GlobalKey<FormState> loginFormKey = GlobalKey<FormState>();
  final userController = Get.put(UserController());

  @override
  void onInit() {
    email.text = localStorage.read('REMEMBER_ME_EMAIL') ?? '';
    // if dont want to save password can skip this
    // password.text = localStorage.read('REMEMBER_ME_PASSWORD');

    super.onInit();
  }

  // email & password sign in
  Future<void> emailAndPasswordSignIn() async {
    try {
      // start loading
      NFullScreenLoader.openLoadingDialog(
          'Logging you in...', NImages.lightNameAppLogo);

      // check internet connectivity
      final isConnected = await NetworkManager.instance.isConnected();
      if (!isConnected) {
        // remove loader
        NFullScreenLoader.stopLoading();
        return;
      }

      // form validation
      if (!loginFormKey.currentState!.validate()) {
        // remove loader
        NFullScreenLoader.stopLoading();
        return;
      }

      // save data if remember me is selected
      if (rememberMe.value) {
        localStorage.write('REMEMBER_ME_EMAIL', email.text.trim());
        localStorage.write('REMEMBER_ME_PASSWORD', password.text.trim());
      }

      // login user using email & password auth
      // ignore: unused_local_variable
      final userCredentials = await AuthenticationRepository.instance
          .loginWithEmailAndPassword(email.text.trim(), password.text.trim());

      // remove loader
      NFullScreenLoader.stopLoading();

      // Redirect
      AuthenticationRepository.instance.screenRedirect();
    } catch (e) {
      NFullScreenLoader.stopLoading();
      NLoaders.errorSnackBar(title: 'Oh Snap!', message: e.toString());
    }
  }

  // google signin authentication
  Future<void> googleSignIn() async {
    try {
      // start loading
      NFullScreenLoader.openLoadingDialog(
          'Logging you in...', NImages.lightNameAppLogo);

      // check internet connectivity
      final isConnected = await NetworkManager.instance.isConnected();
      if (!isConnected) {
        NFullScreenLoader.stopLoading();
        return;
      }

      // google auth
      final userCredentials =
          await AuthenticationRepository.instance.signInWithGoogle();

      // save user record
      await userController.saveUserRecord(userCredentials);

      // remove loader
      NFullScreenLoader.stopLoading();

      // redirect
      AuthenticationRepository.instance.screenRedirect();
    } catch (e) {
      NLoaders.errorSnackBar(title: 'Oh Snap!', message: e.toString());
    }
  }
}

authentication_repository.dart

// google sign in
  Future<UserCredential?> signInWithGoogle() async {
    try {
      // trigger the auth flow
      final GoogleSignInAccount? userAccount = await GoogleSignIn().signIn();

      // obtain auth details from request
      final GoogleSignInAuthentication? googleAuth =
          await userAccount?.authentication;

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

      // once signed in, return UserCredential
      return await _auth.signInWithCredential(credentials);
    } on FirebaseAuthException catch (e) {
      throw NFirebaseAuthException(e.code).message;
    } on FirebaseException catch (e) {
      throw NFirebaseException(e.code).message;
    } on FormatException catch (_) {
      throw const NFormatException();
    } on PlatformException catch (e) {
      throw NPlatformException(e.code).message;
    } catch (e) {
      if (kDebugMode) print('Something went wrong: $e');
      return null;
    }
  }

There’s no error and I also tried reconnecting to Firebase but it didn’t work. I need help ;-;

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img