SM_Berwari
SM_Berwari

Reputation: 691

Flutter : Flutter Shared Preference don't work ,not save username Edit#1

I have an app contain login and register system ,the system is working correctly. the problem is I want to keep user logged in by using flutter_session package but don't work. first I have preloading page contain checking if user is logged in by using functions below :

void gotoTabPae() {
    print('ok');
    Future.delayed(const Duration(milliseconds: 3000), () {
      Navigator.of(context).pushReplacementNamed('tabs');
    });
  }

  void gotoLoginPage() {
    print('no');
    Future.delayed(const Duration(milliseconds: 3000), () {
      Navigator.of(context).pushReplacementNamed('login');
    });
  }

  getuser() async {
    var loginedUser;
    SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
  loginedUser= preferences.getString('username');
});
    loginedUser != null ? gotoTabPae() : gotoLoginPage();
  }

  @override
  void initState() {
    getuser();
    super.initState();
  }

when I run the app and login then when I restart the app it must go to 'tabs' page ,but the value of username is always null therefore it load login page ,the login function is :

login() async {
    var formdata = formLoginKey.currentState;
    if (formdata.validate()) {
      formdata.save();

      var data = {'username': username.text, 'password': password.text};
      var url =xxxx/api/controller/users/login_user.php";
      var response = await http.post(url, body: data);
      var responsebody = jsonDecode(response.body);
      if (responsebody['status'] == 'success') {
        SharedPreferences pref = await SharedPreferences.getInstance();
      pref.setString('username', username.text);
        Navigator.of(context).pushReplacementNamed('tabs');
      } else {
        _showDialog(context, responsebody['status']);
      }
    } else {
    }
  }

But in tabs page it is load the session username corret :

  getuser() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
var logineduser = preferences.getString('username');
  }

  @override
  void initState() {
    getuser();
    super.initState();
  }

How can I solve this? where is my mistake ?

Upvotes: 0

Views: 463

Answers (2)

Dung Ngo
Dung Ngo

Reputation: 1452

I think that in your login function, pref.setString('username', username.text) is run before the response is received. Can you try this:

http.post(url, body: data).then((response) {
  var responsebody = jsonDecode(response.body);
  if (responsebody['status'] == 'success') {
    SharedPreferences pref = await SharedPreferences.getInstance();
    pref.setString('username', username.text);
    Navigator.of(context).pushReplacementNamed('tabs');
  }
});

and let me know the result?

Upvotes: 0

Simran Sharma
Simran Sharma

Reputation: 892

Your code is running the getuser() method in the initSate() method that too even before the super.initState();. That is the reason the value is no able to load which makes it null. You should use it in the build function. Your code might look like this:

@override
  Widget build(BuildContext context) {
    var loginedUser;
    loginedUser = await FlutterSession().get('username');
    loginedUser != null ? return ClassName()(tabs.dart) : return ClassName()(login.dart);
  }

Upvotes: 1

Related Questions