简体   繁体   中英

Unhandled Exception: NoSuchMethodError: The getter 'isEmailVerified' was called on null

I am getting the above mentioned error when logging in. This exception is raised even after I am ensuring that the isEmailVerified() is called only after checking whether the current user is null or not.

My authentication.dart file looks like below:

import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';

abstract class BaseAuth {
  Future<String> signIn(String email, String password);

  Future<String> signUp(String email, String password);

  Future<FirebaseUser> getCurrentUser();

  Future<void> sendEmailVerification();

  Future<void> signOut();

  Future<bool> isEmailVerified();
}
class Auth implements BaseAuth {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  Future<String> signIn(String email, String password) async {
    AuthResult result = await _firebaseAuth.signInWithEmailAndPassword(
        email: email, password: password);
    FirebaseUser user = result.user;
    return user.uid;
  }

  Future<String> signUp(String email, String password) async {
    AuthResult result = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email, password: password);
    FirebaseUser user = result.user;
    sendEmailVerification();
    return user.uid;
  }

  Future<FirebaseUser> getCurrentUser() async {
    FirebaseUser user = await _firebaseAuth.currentUser();
    return user;
  }

  Future<void> signOut() async {
    return _firebaseAuth.signOut();
  }

  Future<void> sendEmailVerification() async {
    FirebaseUser user = await _firebaseAuth.currentUser();
    user.sendEmailVerification();
  }

  Future<bool> isEmailVerified() async {
    FirebaseUser user = await _firebaseAuth.currentUser();
    return user.isEmailVerified;
  }
}

And inside the LoginPage , I am checking like this:

if(auth.signIn(email, password)!=null)
      {
        if(auth.getCurrentUser()!=null)
          {
            if(auth.isEmailVerified() != null) {
              Toast.show("Login Successful!", context, duration: Toast.LENGTH_SHORT, gravity:  Toast.BOTTOM);
              Route route = MaterialPageRoute(builder: (context) => HomePage());
              Navigator.pushReplacement(context, route);
            }
          }
      }

I don't know how to resolve this. Any help is appreciated.

From your Auth class, your methods have future signatures.
You will have to await their results since it will be needed by the other conditions.

You can do:

if((await auth.signIn(email, password))!=null){
      if((await auth.getCurrentUser())!=null){
        if((await auth.isEmailVerified()) != null) {
          Toast.show("Login Successful!", context, duration: Toast.LENGTH_SHORT, gravity:  Toast.BOTTOM);
          Route route = MaterialPageRoute(builder: (context) => HomePage());
          Navigator.pushReplacement(context, route);
        }
      }
    }

But the snippet above must be placed in within an async method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM