Home > Software design >  Flutter gives error when trying to call reload method
Flutter gives error when trying to call reload method

Time:07-23

I'm building a flutter Android application and I'm using Firebase Authentication for managing the user.

Email Verify view

See the above email verify view.

I'm trying to refresh the details of my user. Whether the user is verified or not on pressing the continue button. But I'm getting the following errors:

I'm getting the below message for await user.reload();

The method 'reload' can't be unconditionally invoked because the receiver can be 'null'.

I'm getting the below message for user = await AuthService.firebase().currentUser();

The function can't be unconditionally invoked because it can be 'null'.

My code for OnPressed :

onPressed: () async {
  var user = AuthService.firebase().currentUser;
  await user.reload();
  user = await AuthService.firebase().currentUser();
  if (user?.isEmailVerified ?? false) {
    // user's email is verified
    Navigator.of(context).pushNamedAndRemoveUntil(
      passwordsRoute,
      (route) => false,
    );
  } else {
    // user's email is NOT verified
    Navigator.of(context).pushNamedAndRemoveUntil(
      verifyEmailRoute,
      (route) => false,
    );
  }
},

CodePudding user response:

Here the user can be null so you can use

await user?.reload();

CodePudding user response:

Because Your user is get null when you reload your user. just remove this line

await user.reload();

because without this your code can work also. your error is resolved.

  • Related