0

Im trying to build a Google sign in page and i got this error : type 'Null' is not a subtype of type 'AuthViewModel' in type cast , i dont know what to doh ere I tried to solve it but nothing worked and vscode didnt thro any errors till i run the codes if anyone could help me with this im thankfull for u guys

this is my code:

this is the AuthViewModel that the error tell me the null is not type of it :

class AuthViewModel extends GetxController {
  GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ["email"]);
  FirebaseAuth _auth = FirebaseAuth.instance;

  void googleSignInMethod() async {
    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
  }
}

and this is my log in page:

class LoginScreen extends GetWidget<AuthViewModel> {
  const LoginScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: Colors.white,
      ),
      backgroundColor: Colors.white,
      body: Padding(
        padding: const EdgeInsets.only(top: 50, left: 20, right: 20),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: const [
                CoustumText(
                  text: "Welcom",
                  size: 30,
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
                CoustumText(
                  text: "Sign up",
                  size: 15,
                  color: Colors.greenAccent,
                  fontWeight: FontWeight.w500,
                ),
              ],
            ),
            const SizedBox(
              height: 10,
            ),
            const CoustumText(
              text: "Sign in to continu",
              size: 15,
              color: Colors.grey,
              alignment: Alignment.topLeft,
            ),
            const SizedBox(height: 40),
            CoustumTextFormField(
              text: "Email",
              hint: "aaaaaa@gmail.com",
            ),
            const SizedBox(height: 25),
            CoustumTextFormField(
              text: "password",
              hint: "********",
            ),
            const SizedBox(
              height: 20,
            ),
            const CoustumText(
              text: "forgot password ?",
              size: 15,
              color: Colors.black,
              alignment: Alignment.topRight,
            ),
            const SizedBox(
              height: 20,
            ),
            CoustumButton(
              text: "SIGN IN",
              onPressed: () {},
              size: 20,
            ),
            const SizedBox(
              height: 40,
            ),
            const CoustumText(
              text: "or",
              size: 25,
              color: Colors.black,
              alignment: Alignment.center,
            ),
            TextButton(
              style: TextButton.styleFrom(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(50),
                ),
              ),
              onPressed: () {},
              child: Padding(
                padding: const EdgeInsets.only(right: 35),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Image.asset(
                      "images/facebook.png",
                      width: 60,
                      height: 60,
                    ),
                    const CoustumText(
                        text: "sign in with facebook",
                        size: 17,
                        color: Colors.black)
                  ],
                ),
              ),
            ),
            TextButton(
              style: TextButton.styleFrom(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(50),
                ),
              ),
              onPressed: () {
                controller.googleSignInMethod();
              },
              child: Padding(
                padding: const EdgeInsets.only(right: 35),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Image.asset(
                      "images/google.png",
                      width: 50,
                      height: 50,
                    ),
                    const CoustumText(
                        text: "sign in with Google",
                        size: 17,
                        color: Colors.black)
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
Yousif 304
  • 23
  • 4

1 Answers1

0

Try matching this code it's worked for me

void googleSignInMethod() async {
    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();

    GoogleSignInAuthentication googleSignInAuthentication =
        await googleUser!.authentication;
    final AuthCredential credential = GoogleAuthProvider.credential(
      idToken: googleSignInAuthentication.idToken,
      accessToken: googleSignInAuthentication.accessToken,
    );

    await _auth.signInWithCredential(credential).then((user) async {
      saveUserSocial(user, user.user!.displayName.toString());
      Get.offAll(ControlView());
    });
    // print(userCredential.user);
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77