0

Im trying to check if user verify his mail and then let logged in if not then not. After trying all solution I come to this one . Im using stream on onauthchanges so I know its difficult but here's what Im trying I have a wrapper ''home'' so if user is null im returning authenticate page , if and if user email verified im trying to returning homepage. I found that bool on the internet , but when im starting my app im getting stack overflow error in my wrapper class maybe anyone can check:

class Wrapper extends StatelessWidget {
  static const route='/Wrapper';
  bool get isEmailVerified => isEmailVerified;

  @override
  Widget build(BuildContext context) {
    //Auth.auth().currentUser.isEmailVerified;
    //final user = Provider.of<UserCredential>(context);

    final user = Provider.of<User>(context);



    if(user== null ){
      return Authenticate();
    }else if(isEmailVerified==true){
      return Homepage();
    }else{
      return Resedpasswort();
    }
    //return Home or Authenticate widget
  }
}

The resedpasswort page is for just to see if there's an error when all working im deleting this. Heres my auth page:


class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  String error;

  //Future<bool> isEmailVerified();

  //create user obj based on FirebasedUser
  Model.User _userFromFirebaseUser(User user) {
    return user != null ? Model.User(uid: user.uid) : null;
  }

  //auth change user stream
  Stream<Model.User> get user {
    return FirebaseAuth.instance.authStateChanges().map(_userFromFirebaseUser);
  }

//sign in anon
  Future signInAnon() async {
    try {
      UserCredential result = await _auth.signInAnonymously();
      User user = result.user;

      return _userFromFirebaseUser(user);
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  //sign in with passwort and email
  Future signIN(String email, String password) async {
    try {
      //if (FirebaseAuth.instance.currentUser.emailVerified) {print('fuckyooooooo');
      UserCredential result = await _auth.signInWithEmailAndPassword(
          email: email.trim(), password: password);
      User user = result.user;
      return _userFromFirebaseUser(user);
    }

    on FirebaseAuthException catch (e) {
      switch (e.code) {
        case 'invalid-email':
          {
            return 'Email is not valid';
          }
        case 'user-disabled':
          {
            return 'Account is not active';
          }
        case 'user-not-found':
          {
            return 'No user found';
          }
        case 'wrong-password':
          {
            return 'wrong password';
          }
        default:
          {
            return 'Please verify you account and restart the App';
          }
      }
    }

    return null;
  }


//register with passwort an email
  Future signUp(String email, String password,) async {
    try {
      UserCredential result = await _auth.createUserWithEmailAndPassword(
          email: email, password: password);

      User user = result.user;
      await user.sendEmailVerification();
      return _userFromFirebaseUser(user);
      //await DatbaseService(uid:user.uid).updateUserData('0','new crew member','100','dfdssf');
      //  return ;

      //user.sendEmailVerification();
      // ( await DatbaseService(uid:user).updateUserData('0','new crew member','100','dfdssf')).user.uid;;
    } on FirebaseAuthException catch (e) {
      switch (e.code) {
        case 'invalid-email':
          {
            return 'Email is not valid';
          }
        case 'user-disabled':
          {
            return 'Account is not active';
          }
        case 'user-not-found':
          {
            return 'No user found';
          }
        case 'wrong-password':
          {
            return 'wrong password';
          }
        default:
          {
            return 'Unexpected error!';
          }
      }
    }
    return null;
  }

  //resetpassword
  Future sendPasswordResetEmail(String email) async {
    try {
      return await _auth.sendPasswordResetEmail(email: email);
    } on FirebaseAuthException catch (e) {
      switch (e.code) {
        case 'invalid-email':
          {
            return 'Email is not valid';
          }
        case 'user-disabled':
          {
            return 'Account is not active';
          }
        case 'user-not-found':
          {
            return 'No user found';
          }
        case 'wrong-password':
          {
            return 'wrong password';
          }
        default:
          {
            return 'Unexpected error!';
          }
          return null;
      }
    }
  }

  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

Heres my future method

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

2 Answers2

0

It seems you are calling isEmailVerified method recursively. That causes the app to crash (stack overflow error). Provide method definition for isEmailVerified

Madhan
  • 305
  • 1
  • 5
  • 10
0

This is a recursive definition:

bool get isEmailVerified => isEmailVerified

In words it's saying: to get the value of isVerified, get the value of isVerified, which is pretty much the definition of recursion.

In your code, this line is completely meaningless anyway, so if you just remove it, the recursion (and the error) will disappear.


Alternatively, it could be that you wanted it to be:

bool get isEmailVerified => Auth.auth().currentUser.isEmailVerified;

So in this case you're defining a local read-only property isEmailVerified what is a short-hand notation for Auth.auth().currentUser.isEmailVerified.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • OK I`m sorry the first suggestion was right. So the stack overflow error is away. But what can I do now, because it just show me the resedpasswort page, it should show me the authenticate page because I have no user inside my Firebase Console . –  Mar 11 '21 at 08:07
  • This bool is automatically return true when somebody verify his mail or do I understand it wrong ? But anyways it should return the authenticate, because of no user. Please help. –  Mar 11 '21 at 08:10
  • 1) You may have more problems in the code, but I only focused on the error message from your title. If you still have problems after this fix, I recommend posting a new question for that problem with its own [MCVE](http://stackoverflow.com/help/mcve). 2) Note that it may take up to an hour before the app picks up the user's verification status after they click the link, because the verification happens outside of the app. If that is indeed the behavior you're still asking about, see https://stackoverflow.com/questions/41326951/firebase-email-verification-behavior – Frank van Puffelen Mar 11 '21 at 15:32
  • Hey Frank, thank you for your answer. I think I got the right solution how to handle this please check my newest post on my Profil. The name of the question is "How to use Provider?". So I figure out what I should call when I trying to check if user verify his mail. it is this : "if(FirebaseAuth.instance.currentUser.emailVerified)" please check my post thanks –  Mar 11 '21 at 15:40