首页 > 解决方案 > 如何在颤振中使用 sharedPreference 以使用 setBool 和 GetBool 保持用户在颤振中登录

问题描述

我正在颤振中练习电子邮件身份验证,几乎一切都结束了。现在,我想使用 sharedPreference 让用户保持登录状态。我尝试了一些方法,但没有得到结果。我正在使用 bool 类型来获取用户是否登录。但我对此很陌生,你能帮我吗?有什么我遗漏的吗?

这是我正在使用的 sharedPreference 静态类

class sharedPreference {
  static String sharedPreferenceUserLoggedInKey = 'userLoggedIn';
  static String sharedPreferenceUserSignedUpKey = 'userSignedUp';

  //saving data to sharedPreference
  static Future<bool> saveUserLoggedInSharedPreference(
      bool isUserLoggedIn) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setBool(sharedPreferenceUserLoggedInKey, isUserLoggedIn);
  }

  static Future<bool> saveUserSignedUpSharedPreference(
      bool isUserSignUp) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setBool(sharedPreferenceUserSignedUpKey, isUserSignUp);
  }

  //getting data to sharedPreference
  static Future<bool> getUserLoggedInSharedPreference() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.getBool(sharedPreferenceUserLoggedInKey);
  }

  static Future<bool> getUserSignedUpSharedPreference() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.getBool(sharedPreferenceUserSignedUpKey);
  }

}

这是触发 setBool 的 signIn 按钮:SignInButton:

FlatButton(
onPressed: ()
{
HelperFunction.saveUserLoggedInSharedPreference(true);
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => DashBoard(email: email),
          ),
})

主要功能

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light
      .copyWith(systemNavigationBarColor: Colors.black));

  runApp(
    DevicePreview(
      enabled: kReleaseMode,
      builder: (context) => FlashChat(),
    ),
  );
}

class FlashChat extends StatefulWidget {
  @override
  _FlashChatState createState() => _FlashChatState();
}

class _FlashChatState extends State<FlashChat> {
  bool isUserLoggedIn;
  bool isUserSignedUp;

  void getLoggedInStatus() async {
    await HelperFunction.getUserLoggedInSharedPreference().then((value) {
      isUserLoggedIn = value;
    });
  }

  void getSignedUpStatus() async {
    await HelperFunction.getUserSignedUpSharedPreference().then((value) {
      isUserSignedUp = value;
    });
  }

  @override
  void initState() {
    getLoggedInStatus();
    getSignedUpStatus();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
          initialRoute: isUserLoggedIn == true
              ? DashBoard.id: WelcomeScreen.id,
          routes: {
            WelcomeScreen.id: (context) => WelcomeScreen(),
            LoginScreen.id: (context) => LoginScreen(),
            RegistrationScreen.id: (context) => RegistrationScreen(),
            DashBoard.id: (context) => DashBoard(),
          },
          debugShowCheckedModeBanner: false,
        );
      });
    });

标签: firebasefluttersharedpreferences

解决方案


当用户获得登录集时

 prefs.setBool("isLogin", True);

当用户在注销功能中注销时

 pref.clear()

并在启动画面或开始时放置此逻辑

      SharedPreferences prefs = await SharedPreferences.getInstance();
    var isLogin = prefs.getBool("isLogin");

if (isLogin)
{
//Navigate user to the required screen
}
else{
//navigate user to login screen
}

推荐阅读