How can I store the list with sharedPreferences and use it in the ListView? this is my shared preferences codeand this is my list view
this is my code:
late SharedPreferences _prefs;
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((prefs) {
setState(() => _prefs = prefs);
});
}
_saveList(List<String> list) {
_prefs.setStringList('key', list);
print('$_prefs');
}
List _fetchList() {
print('$_prefs');
return _prefs.getStringList('key') ?? [];
}
Widget build(BuildContext context) {
_saveList(list);
return MaterialApp(
title: 'Errori',
home: Scaffold(
appBar: AppBar(
title: const Text('Report Errori'),
),
body: Container(
child: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: _fetchList().length,
itemBuilder: (context, index) => Container(
child: Material(
borderRadius: BorderRadius.circular(5.0),
child: Center(
child: Text(_fetchList()[index]),
),
))))
],
),
),
),
);
I don't think what I did is correct. I keep having the problem that when I restart the app, the items disappear