I am doing some charge information with shared preference but in iOS is not working while in Android is working as expected
@override
void initState() {
super.initState();
readData();
}
I have 2 textfields for fill it up when readData() have data
final email = TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
initialValue: _email,
validator: (input) {
if(input.isEmpty) {
return 'Introduce un Email';
}
},
onSaved: (input) => _email = input,
decoration: InputDecoration(
hintText: 'Email',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
final password = TextFormField(
autofocus: false,
obscureText: true,
initialValue: _password,
validator: (input) {
if(input.isEmpty) {
return 'Introduce la contraseña';
}
},
onSaved: (input) => _password = input,
decoration: InputDecoration(
hintText: 'Contraseña',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
readData() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_email = prefs.getString('email');
_password = prefs.getString('password');
});
}
In android works well but not in iOS
I think the right way to update the value of the textfields is to use a TextEditingController
.
Your code would look like this:
In your state
final TextEditingController emailCtrl = TextEditingController();
final TextEditingController passwordCtrl = TextEditingController();
readData() async {
final prefs = await SharedPreferences.getInstance();
emailCtrl.text = prefs.getString('email');
password.text = prefs.getString('password');
}
Your TextFields
final email = TextFormField(
// ... your other arguments
controller: emailCtrl
);
final password = TextFormField(
// ... your other arguments
controller: passwordCtrl
);