snapshot.data.emailVerified 用户 flutter firebase 验证电子邮件后,不会实时更改屏幕

snapshot.data.emailVerified not changing the screen in realtime after the email has been verified by user flutter firebase

我想在用户验证电子邮件后实时导航到主屏幕,但在 用户已经验证了他们的电子邮件,它仍然保留在 VerifyEmailScreen 上。

直到现在,只有当用户注销或登录时,我才能实时导航到登录屏幕和主屏幕

我尝试在 3 后重新加载给用户,但它仍然在 EmailVerification 屏幕 .....请帮忙

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Timer timer;
  @override
  void initState() {
    timer = Timer(Duration(seconds: 3), () {
      FirebaseAuth.instance.currentUser.reload();
    });
    super.initState();
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      child: MaterialApp(
          darkTheme: ThemeData(
            brightness: Brightness.dark,
          ),
          themeMode: ThemeMode.dark,
          debugShowCheckedModeBanner: false,
          home: LandingPage()
      ),
      providers: [
        ChangeNotifierProvider(create: (_)=>FirebaseMethods()),
        ChangeNotifierProvider(create: (_)=>ProfileScreenUtils())
      ],
    );
  }
}

class LandingPage extends StatelessWidget {

  //final FirebaseRepository _repository = FirebaseRepository();
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<User>(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, AsyncSnapshot<User> snapshot) {
            if (snapshot.hasData) {
           // print("USER ID IF USER IS NOT NULL :  " + user.uid);
              if(snapshot.data.emailVerified)
                {
                  return HomeScreen();
                }
              else
                {
                  return VerifyEmailScreen();
                }
            }
            else if(snapshot.hasError)
              {
                return CircularProgressIndicator();
              }
            else {
              //print("USER ID IF THE USER IS NULL :  " + user.uid);
              return LoginScreen();
            }
      },
    );
  }
}

当用户验证电子邮件时,不会调用 authStateChanges。 您应该调用 FirebaseAuth.instance.currentUser.reload(); 来获取用户的刷新数据。 您可以使用轮询请求来做到这一点,例如每分钟一次:

Timer timer = Timer(Duration(seconds: 1), () {
  FirebaseAuth.instance.currentUser.reload();
});

或者您可以添加一个按钮,用户在验证电子邮件时点击该按钮。