Am using the package shared_preferences in my futter application to save login..
pubspec.yaml
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
localize_and_translate: ^1.1.1+6
flutter_fadein: ^1.1.1
http: ^0.12.2
loading_overlay: ^0.2.1
fluttertoast: ^7.1.1
image_picker: ^0.6.7+12
shared_preferences: ^0.5.12+4 // this line
Saving the data
Future saveLoginData(LoginModel loginModel) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
print(loginModel.token); //gives me the right string
prefs.setString('theToken', loginModel.token);
print(prefs.getString('theToken')); //gives me the right string
prefs.setBool('logged', true);
prefs.setString('type', 'vendor');
prefs.setString('mobileNumber', loginModel.username);
}
recalling the data
// this function is called while starting the login screen ,to see if the user have to login or just can skip tothe home page
Future<String> checkIfLogged() async {
return await SharedPreferences.getInstance().then((prefs) {
String theToken = prefs.getString('theToken');
print(theToken); //outputs null
return theToken; //returns null
});
}
When trying to call the saved data in the same opened instance of the app it is being called perfectly, but when the app is closed and checking for data at the initstate of the login screen. it gives back null to everything!! a