从匿名firebase隐藏用户,但赢得';t刷新屏幕抖动

从匿名firebase隐藏用户,但赢得';t刷新屏幕抖动,firebase,flutter,dart,firebase-authentication,flutter-layout,Firebase,Flutter,Dart,Firebase Authentication,Flutter Layout,我已在下面设置了转换用户代码: 当我输入电子邮件和密码并单击注册时,firebase会进行更改,但屏幕不会转到ProfileScreen,它只是保持加载状态。一旦我重新启动应用程序,它就会显示正确的页面 //create User object based on firebase user User _userFromFirebaseUser(FirebaseUser user){ return user != null ? User(uid: user.uid, email:

我已在下面设置了转换用户代码: 当我输入电子邮件和密码并单击注册时,firebase会进行更改,但屏幕不会转到ProfileScreen,它只是保持加载状态。一旦我重新启动应用程序,它就会显示正确的页面

  //create User object based on firebase user
  User _userFromFirebaseUser(FirebaseUser user){
    return user != null ? User(uid: user.uid, email: user.email) : null;
  }

  //convert users from anonymous
  Future convertUserWIthEmail(String email, String password) async {
    final currentUser = await _auth.currentUser();

    final credential = EmailAuthProvider.getCredential(email: email, password: password);
    await currentUser.linkWithCredential(credential);
    return _userFromFirebaseUser(currentUser);
  }
配置文件屏幕包装器

class ProfileWrapper extends StatelessWidget {
  static const String id = 'profile_wrapper';

  @override
  Widget build(BuildContext context) {

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

    if (user.email != null){
      return ProfileScreen();
    } else {
      return ProfileRegister();
    }

  }
}

只需将
setState
移动到
if
条件之前。这是因为在
\u authNew.convertUserWIthEmail(
)之后,您没有告诉flatter更新您的UI,并且只有在发生错误时才调用
setState
(因此,也删除了加载)

无论是否存在错误或成功,您都希望删除加载状态,因此,基本上:

 if (_formKey.currentState.validate()) {
                                    setState(() {
                                      loading = true;
                                    });
                                    dynamic result = await _authNew
                                        .convertUserWIthEmail(
                                        email, password);
                                      setState(() {
                                        loading = false;
                                      });
                                    if (result == null) {
                                       error = 'Please enter a valid email and password';
                                        print('ERROR HAPPENED');
                                      _scaffoldKey.currentState.showSnackBar(
                                          SnackBar(
                                            backgroundColor: Colors.red,
                                            content: new Text(error,
                                              style: kErrorMessageTextStyle,
                                              textAlign: TextAlign.center,),
                                            duration: new Duration(seconds: 3),
                                          )
                                      );
                                    }

这并没有解决问题,它只是去掉了加载屏幕。包装器控制显示哪个屏幕。问题是user.email在热刷新之前是空的,即使我转到应用程序中的另一个页面并返回到配置文件页面,user.email仍然是空的。
 if (_formKey.currentState.validate()) {
                                    setState(() {
                                      loading = true;
                                    });
                                    dynamic result = await _authNew
                                        .convertUserWIthEmail(
                                        email, password);
                                      setState(() {
                                        loading = false;
                                      });
                                    if (result == null) {
                                       error = 'Please enter a valid email and password';
                                        print('ERROR HAPPENED');
                                      _scaffoldKey.currentState.showSnackBar(
                                          SnackBar(
                                            backgroundColor: Colors.red,
                                            content: new Text(error,
                                              style: kErrorMessageTextStyle,
                                              textAlign: TextAlign.center,),
                                            duration: new Duration(seconds: 3),
                                          )
                                      );
                                    }