【问题标题】:Coverting user from anonymous firebase, but won't refresh screen flutter从匿名firebase转换用户,但不会刷新屏幕颤动
【发布时间】:2020-03-29 19:58:01
【问题描述】:

我在下面设置了转换用户代码: 当我输入电子邮件和密码并单击注册时,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();
    }

  }
}

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

    RoundedButton(
                                title: 'Register',
                                colour: Colors.white,
                                textColour: kMainColour,
                                onPressed: () async {
                                  if (_formKey.currentState.validate()) {
                                    setState(() {
                                      loading = true;
                                    });
                                    dynamic result = await _authNew
                                        .convertUserWIthEmail(
                                        email, password);
                                    if (result == null) {
                                      setState(() {
                                        loading = false;
                                        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),
                                          )
                                      );
                                    }
//                                  setState(() {
//                                    showSpinner = true;
//                                  });

                                  }
                                }),

【问题讨论】:

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


    【解决方案1】:

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

    无论有错误还是成功都想去掉remove加载状态,所以,基本上:

     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 仍然为空。
    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 2023-04-05
    • 2016-12-06
    • 2021-01-24
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多