0

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

idontknow
  • 1
  • 1

2 Answers2

0

To store list of string you can use the following:

List<String> stringsList=  ['Alex', 'Mob', 'Flutter'];

final SharedPreferences prefs = await SharedPreferences.getInstance();

// store your string list in shared prefs                
prefs.setStringList("stringList", stringsList);

To retrieve list of string you can use the following:

final SharedPreferences prefs = await SharedPreferences.getInstance();

List<String> myStringList = (prefs.getStringList('stringList') ?? List<String>());
Md. Kamrul Amin
  • 1,870
  • 1
  • 11
  • 33
  • i have methods to save and to return the list in sharedpreferences but i don't understand how to recall them in the listview – idontknow May 09 '22 at 12:01
  • Use listview.builder. You will see a parameter called itemCount. Just write there "myStringList.length". This will calculate the length of your list and show items appropriately. For more info on how to code a list please check the link https://www.geeksforgeeks.org/listview-class-in-flutter/#:~:text=In%20Flutter%2C%20ListView%20is%20a,ListView. – Md. Kamrul Amin May 09 '22 at 12:08
  • yeah, I did this but when i restart app disappear all item in listview – idontknow May 09 '22 at 13:31
  • Where do you get your list data from? Is the data static? – Md. Kamrul Amin May 10 '22 at 05:05
  • no, I noticed yesterday that with static data it works without problems while I need a dynamic list so it doesn't work – idontknow May 10 '22 at 16:46
  • Now work on restart but not when i close app and re open it – idontknow May 10 '22 at 19:07
0

The answer to your question is here to save a list of objects to sharedpreferences and you can recall them in the commented place below:

itemBuilder(_,_){ 
//Recall the listview method 
return yourWidget;}