many thanks for helping me! I am learning Flutter, and I have encountered a problem: My SharedPreferences is not working when I close my app and open it again (it is just the default counter app). Here is my code:
@override
void initState() {
super.initState();
_loadScore();
}
void _loadScore() async {
final SharedPreferences scoreData = await SharedPreferences.getInstance();
setState(() {
_score = scoreData.getInt('score') ?? 0;
});
}
void _incrementCounter() async {
final SharedPreferences scoreData = await SharedPreferences.getInstance();
setState(() {
_score = (scoreData.getInt('score') ?? 0) + 1;
scoreData.setInt('score', _score);
});
}