Using SharedPreferences to set login state and retrieving it at App launch - Flutter

前端 未结 1 1332
萌比男神i
萌比男神i 2021-01-16 14:47

I have an flutter app in which I have to check the login status when the app is launched and call the relevant screen accordingly.

The code used to launch the app:

相关标签:
1条回答
  • 2021-01-16 15:48

    You can use FutureBuilder to solve this problem.

    @override
    Widget build(BuildContext context) {
     new FutureBuilder<String>(
        future: getLoginState(),
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.active:
            case ConnectionState.waiting:
              return new Text('Loading...');
            case ConnectionState.done:
              if (snapshot.hasData) {
                loginState = snapshot.data;
                if(loginState) {
                 return Container(child: Text('Logged In'));
                }
                else {
                 return Container(child: Text('Not Logged In));
                }
              } else {
               return  Container(child: Text('Error..));
              }
          }
        },
      )
     }
    

    Note: we don't need isLoggedIn state variable.

    0 讨论(0)
提交回复
热议问题