首页 > 解决方案 > 颤动的firebase注销失败

问题描述

状态管理太难了。

通过firebase登录正常工作,第一次注销工作正常。

但是,如果您登录并转到另一个屏幕,则无法注销。

什么地方出了错?

'''

Future logout() async {
    await firebaseAuth.signOut();
  }

'''

'''

Widget build(BuildContext context) {
    final loginProvider = Provider.of<AuthProvider>(context);
    return Scaffold(
        appBar: AppBar(
          actions: [
            IconButton(
              onPressed: () async => await loginProvider.logout(),
              icon: Icon(
                Icons.settings,
                color: Colors.white,
              ),
            )
         
      
        ),

如果我像下面这样修改它,它工作正常。但我不知道这是否是正确的方法。

'''

onPressed: () async => await loginProvider.logout().then(
                  (value) => Navigator.of(context).pushReplacement(
                      MaterialPageRoute(
                          builder: (BuildContext context) => Wrapper()))),

'''

Future login(String email, String password) async {
    setLoading(true);
    try {
      UserCredential authResult = await firebaseAuth.signInWithEmailAndPassword(
          email: email, password: password);
      User? user = authResult.user;
      setLoading(false);
      return user;
    } on SocketException {
      setMessage('No internet');
    } catch (e) {
      setLoading(false);
      setMessage(e.toString());
    }
    notifyListeners();
  }

'''

'''

'''

Container(
                    height: 50.0,
                    margin: EdgeInsets.only(top: 20.0),
                    width: MediaQuery.of(context).size.width * 0.8,
                    child: MaterialButton(
                        color: Color(0xffe50815),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        onPressed: () async {
                          if (this._formKey.currentState!.validate()) {
                            print('email : ${this.idCt.text}');
                            print('password : ${this.pwCt.text}');
                            await loginProvider.login(
                                this.idCt.text.trim(), this.pwCt.text.trim());
                          }
                        },
                        child: loginProvider.isLoading
                            ? CircularProgressIndicator()
                            : Text("LOGIN",
                                style: new TextStyle(
                                    fontSize: 12.0,
                                    fontWeight: FontWeight.bold,
                                    color: Colors.white))),
                  ),

'''

'''

标签: firebaseflutter

解决方案


使用.then然后导航到包装器并没有错,但您可能希望将onPressed代码重构为:

    onPressed: () async {
      await loginProvider.logout();
      Navigator.of(context).pushReplacement(
        MaterialPageRoute(
          builder: (BuildContext context) => Wrapper())
      );
    },

由于您正在使用async/await,因此仅使用它而不是将其与.then.


推荐阅读