'SharedPreferences in Flutter not persisting?
I'm trying to implement a "Remember Me" functionality by using SharedPreferences. Unfortunately, no matter what I do, it doesn't seem like the login details are persisting. I'm using SharedPreferences on my Sign In screen as below by invoking it in the initState
method (through to my GetX controller):
final SignInController controller = Get.put(SignInController());
@override
void initState() {
// TODO: implement initState
controller.loadRememberMeDetails();
super.initState();
}
...
Row(
children: [
SizedBox(
height: 24.0,
width: 24.0,
child: GetBuilder<SignInController>(
builder: (s) => Checkbox(
value: s.isChecked,
onChanged: s.handleRememberMe,
),
),
),
SizedBox(width: 5.0),
Material(
color: Colors.transparent,
child: InkWell(
onTap: () {
controller.handleRememberMe(
!controller.isChecked);
},
child: Text(
"Remember Me",
style: TextStyle(
color: Colors.black,
fontSize: 15),
),
))
],
),
I invoke the following to save it:
void handleRememberMe(bool? value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool("remember_me", value!);
await prefs.setString('email', emailController.text);
await prefs.setString('password', passwordController.text);
print("Remember me: ${emailController.text} ${passwordController.text}");
isChecked = value!;
update();
}
initState()
invokes the following method on initialisation:
void loadRememberMeDetails() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
var email = prefs.getString("email") ?? "";
var password = prefs.getString("password") ?? "";
var rememberMe = prefs.getBool("remember_me") ?? false;
if (rememberMe) {
isChecked = true;
emailController.text = email;
passwordController.text = password;
update();
}
} catch (e) {
print(e);
}
}
However, email
, etc local variables show null.
Any ideas on how to fix?
Solution 1:[1]
You can use GetStorage to achieve that, is easier to use, adapting to your code, it can be like this:
- In your main.dart:
main() async {
await GetStorage.init();
runApp(App());
}
- Then call your storage space in initState:
@override
void initState() {
// TODO: implement initState
final box = GetStorage();
controller.loadRememberMeDetails();
super.initState();
}
- And in your functions:
void handleRememberMe(bool? value){
box.write('remember_me', value!.toString());
box.write('email', emailController.text);
box.write('password', emailController.text);
print("Remember me: ${emailController.text} ${passwordController.text}");
isChecked = value!;
update();
}
void loadRememberMeDetails(){
try {
var email = box.read("email")??"";
var password = box.read("password")??"";
var rememberMe = box.read("remember_me") == "true" ? true : false;
if (rememberMe) {
isChecked = true;
emailController.text = email;
passwordController.text = password;
update();
}
} catch (e) {
print(e);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Williams Bobadilla |