我如何在flutter应用程序中保存Facebook用户登录会话?

问题描述 投票:0回答:1

我想在我的应用程序中保存用户Facebook登录会话,因此用户不必在每次重新打开应用程序时登录。我已经在Google上完成了此操作,但找不到有关Facebook的任何信息。这是我的验证码。

  Future<void> logInWithFacebook() async {
    final result = await fbLogin.logIn(['email']);

    switch (result.status) {
      case FacebookLoginStatus.loggedIn:
        final token = result.accessToken.token;
        final graphResponse = await http.get(
            'https://graph.facebook.com/v2.12/me?fields=name,picture,email&access_token=${token}');
        final profile = JSON.jsonDecode(graphResponse.body);
        print(profile);
        await Navigator.push(context,MaterialPageRoute(builder: (context)=>HomePage()));
        setState(() {
          userProfile = profile;
          isAuth = true;
        });
        break;

      case FacebookLoginStatus.cancelledByUser:
        setState(() => isAuth = false);
        break;
      case FacebookLoginStatus.error:
        setState(() => isAuth = false);
        break;
    }
  }
flutter dart facebook-login
1个回答
0
投票

在共享首选项中保存您的令牌或登录状态。这是Flutter包的链接

Shared Preference Package

SharedPreferences prefs = await SharedPreferences.getInstance();

我建议将其设置为全局变量,以便与整个应用程序包共享其访问权限。之后,您可以执行以下操作]

prefs.setBool("pref_logged_user",isLoggedIn);

其中pref_logged_user是将用于访问数据的密钥

bool isLoggedIn = await prefs.getBool("pref_logged_user")??false;

??用于检查输出是否为空。如果为null,则返回false;

© www.soinside.com 2019 - 2024. All rights reserved.