-2
I’m learning to flutter these days.
I’m studying Shared Preferences:
When I save to Shared Preferences and retrieve the information, it works, but when I remove and try to retrieve the information, I’d like you to display the text "Worthless". But gives error in the app
Follows part of the code and its functions
TextEditingController _controllerCampo = TextEditingController();
String _textoSalvo = "Nada salvo!";
_salvar() async{
String valorDigitado = _controllerCampo.text;
final prefs = await SharedPreferences.getInstance();
await prefs.setString("nome", valorDigitado);
print("operacao (salvar) $valorDigitado");
}
_recuperar() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_textoSalvo = prefs.getString("nome") ?? "Sem valor";
});
}
_remover() async{
final prefs = await SharedPreferences.getInstance();
prefs.remove("nome");
setState(() {
_textoSalvo = "";
});
}
Right here
_textoSalvo = prefs.getString("nome") ?? "Sem valor";
Here is the snippet to add the text and buttons
TextField(
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: "Digite algo"
),
controller: _controllerCampo,
),
Row(
children: <Widget>[
RaisedButton(
color: Colors.blue,
textColor: Colors.white,
padding: EdgeInsets.all(15),
child: Text(
"Salvar",
style: TextStyle(
fontSize: 15
),
),
onPressed: _salvar,
),
RaisedButton(
color: Colors.blue,
textColor: Colors.white,
padding: EdgeInsets.all(15),
child: Text(
"Recuperar",
style: TextStyle(
fontSize: 15
),
),
onPressed: _recuperar,
),
RaisedButton(
color: Colors.blue,
textColor: Colors.white,
padding: EdgeInsets.all(15),
child: Text(
"Remover",
style: TextStyle(
fontSize: 15
),
),
onPressed: _remover,
)
Makes that mistake
Where can I be missing?
Please enter the complete code. Especially the part of Text you create with the
_textoSalvo
. The error is not in what you showed.– Julio Henrique Bitencourt
I added the other methods
– adventistaam