skip to Main Content

I am trying to get the user’s profile picture, name and email but I keep on getting the _CastError (Null check operator used on a null value) error. This is where I get my error:

final user = FirebaseAuth.instance.currentUser!;//at the end is where the error is (the '!')

Code:

        Column(
          children: [
            CircleAvatar(
              radius: 52,
            backgroundImage: NetworkImage(user.photoURL!),
            ),
            const SizedBox(
              height: 8,
            ),
            Text(
              user.displayName!,
              style: titleStyle,
            ),
            const SizedBox(
              height: 8,
            ),
            Text(
              user.email!,
              style: titleStyle,
            ),
          ],
        ),

I’ve tried moving the ! around but it just throws the error somewhere else then. I’ve tried implementing ??/? on some places but to also no help.

3

Answers


  1. Because currentUser is null, you need to accept null and handle it properly like.

    final user = FirebaseAuth.instance.currentUser;
    // now `user` can accept null and the title error will be gone
    

    Next thing comes when you are using it.

    if(user?.photoURL!=null) CircleAvatar(....
    

    Find more about null-safety.

    Login or Signup to reply.
  2. After watching some videos I figured that it was just a wrong approach and there are better and easier ways of doing it.

    final controller = Get.put(LoginController());
    final _googleSignin = GoogleSignIn();
    var googleAccount = Rx<GoogleSignInAccount?>(null);
    
    login() async {
        googleAccount.value = await _googleSignin.signIn();
      }
    
    Column(
              children: [
                CircleAvatar(
                  radius: 52,
                  backgroundImage:
                      NetworkImage(controller.googleAccount.value?.photoUrl ?? ''),
                ),
                const SizedBox(
                  height: 8,
                ),
                Text(
                  controller.googleAccount.value?.displayName ?? '',
                  style: subHeadingStyle,
                ),
                const SizedBox(
                  height: 8,
                ),
                Text(
                  controller.googleAccount.value?.email ?? '',
                  style: subHeadingStyle,
                ),
                const SizedBox(
                  height: 20,
                )
              ],
            ),
    
    Login or Signup to reply.
  3. // 1. Check if the variable is null before using it:

    if (variable != null) { // use variable safely } else { // handle null case }

    // 2. Use the null-aware operator (?.) instead of the null check operator (!) to avoid the error altogether:

    variable?.doSomething();

    // 3. Use the null-coalescing operator (??) to provide a default value when the variable is null:

    final value = variable ?? defaultValue;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search