【问题标题】:User not able to sign out in firebase Flutter application用户无法在 firebase Flutter 应用程序中退出
【发布时间】:2021-07-30 10:37:15
【问题描述】:

D/FirebaseAuth(7994):通知 id 令牌侦听器有关注销事件。 D/FirebaseAuth(7994):通知身份验证状态侦听器有关注销事件。

class LandingPage extends StatefulWidget {
  @override
  _LandingPageState createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  User _user;

  @override
  void initState() {
    super.initState();
    _updateUser(FirebaseAuth.instance.currentUser);
  }
  void _updateUser(User user) {
    setState(() {
      _user = user;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_user == null) {
      return SignInPage(
        onSignIn: _updateUser,
      );
    }
    return HomePage(
      onSignOut: () => _updateUser(null),
    );
  }
}

首页代码:-

class HomePage extends StatelessWidget {

  HomePage({@required this.onSignOut});
  final VoidCallback onSignOut;
  Future<void> _signOut() async {

    try {
      await FirebaseAuth.instance.signOut();
    } catch (e) {
      print(e.toString()) ;
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
        actions: <Widget>[
          FlatButton(
            child: Text(
              'Logout',
              style: TextStyle(
                  fontSize: 18.0,
                  color: Colors.white,
              ),
            ),
            onPressed: _signOut,

          ),
        ],
      ),
    );
  }
}

这是我的代码,但用户无法退出我的应用程序。有人可以告诉我要修复什么

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    尝试等待signout 呼叫:

    class HomePage extends StatelessWidget {
    
      HomePage({@required this.onSignOut});
      final VoidCallback onSignOut;
      Future<void> _signOut() async {    
        try {
          await FirebaseAuth.instance.signOut();
        } catch (e) {
          print(e.toString()) ;
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Home Page'),
            actions: <Widget>[
              FlatButton(
                child: Text(
                  'Logout',
                  style: TextStyle(
                      fontSize: 18.0,
                      color: Colors.white,
                  ),
                ),
                onPressed: () async => await _signOut(), // Updated code    
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 之前尝试过,但弹出相同的消息
    猜你喜欢
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2014-11-29
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多