简体   繁体   中英

Coverting user from anonymous firebase, but won't refresh screen flutter

I have setup the convert user code below: When I enter an email and password and click register, firebase makes the change but the screen won't go to the ProfileScreen, it just stays on loading. Once I hot restart the app, it shows the correct page.

  //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 SCREEN WRAPPER

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

  }
}

IMPLEMENTATION IN PROFILE_REGISTER PAGE - RoundedButton is a custom class

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

                                  }
                                }),

Just move your setState before the if condition. That happens because you are not telling Flutter to update your UI after _authNew.convertUserWIthEmail( and only calling setState when an error happens (thus, removing the loading as well).

You want to remove the remove the loading state whether there is an error or success, so, basically:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM