覆盖匿名Firebase中的用户,但不会刷新屏幕抖动

时间:2019-12-04 22:56:58

标签: firebase flutter dart firebase-authentication flutter-layout

我在下面设置了转换用户代码: 当我输入电子邮件和密码并单击“注册”时,firebase进行了更改,但该屏幕不会转到ProfileScreen,它将一直处于加载状态。热重启应用后,它会显示正确的页面。

np.array

PROFILE SCREEN WRAPPER

  //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);
  }

在PROFILE_REGISTER页面中的实现- RoundedButton是自定义类

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();
    }

  }
}

1 个答案:

答案 0 :(得分:0)

只需将setState移到if条件之前。之所以发生这种情况,是因为您没有告诉Flutter在_authNew.convertUserWIthEmail(之后更新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),
                                          )
                                      );
                                    }