Flutter: How to listen to the FirebaseUser is Email verified boolean?

后端 未结 8 748
走了就别回头了
走了就别回头了 2021-01-01 20:38

My Idea: I want to use the Firebase Auth Plugin in Flutter to register the users. But before they can access the App, they have to verify their Email addres

8条回答
  •  余生分开走
    2021-01-01 21:33

    I just faced the same situation in my app. My solution was to create a periodic timer into the initState method of a strategic route to hold the app until the e-mail is verified. It is not so elegant as using a listener but works fine.

    bool _isUserEmailVerified;
    Timer _timer;
    
    @override
    void initState() {
        super.initState();
        // ... any code here ...
        Future(() async {
            _timer = Timer.periodic(Duration(seconds: 5), (timer) async {
                await FirebaseAuth.instance.currentUser()..reload();
                var user = await FirebaseAuth.instance.currentUser();
                if (user.isEmailVerified) {
                    setState((){
                        _isUserEmailVerified = user.isEmailVerified;
                    });
                    timer.cancel();
                }
            });
        });
    }
    
    @override
    void dispose() {
        super.dispose();
        if (_timer != null) {
            _timer.cancel();
        }
    }
    

提交回复
热议问题