-1

How can I load all saved in SharedPreferences?

I have saved lots of bools and need to load all to a list!

This is how I save:

    SharedPreferences sharedPreferences;
    bool isfavorit;
    
    @override
      void initState() {
        super.initState();
    
        SharedPreferences.getInstance().then((SharedPreferences sp) {
          sharedPreferences = sp;
          isfavorit = sharedPreferences.getBool('${widget.id}');
          // will be null if never previously saved
          if (isfavorit == null) {
            isfavorit = false;
            persist(isfavorit); // set an initial value
          }
          setState(() {});
        });
      }
    
        void persist(bool value) {
        setState(() {
          isfavorit = value;
        });
        sharedPreferences?.setBool('${widget.id}', value);
      }
OsTeNg24
  • 274
  • 1
  • 15
Marcel
  • 75
  • 1
  • 9

1 Answers1

1
List<bool> prefList = [];
var sharedPreferences = await SharedPreferences.getInstance();
Set<String> keys = sharedPreferences.getKeys();

for(int i=0; i<keys.length ; i++){
  bool value = sharedPreferences.getBool(keys.elementAt(i));
  prefList.add(value);
}

If you have non bool value too in your persistent storage, you have to use the recognisable key to store those value. For example :- sharedPreferences.setBool('bool${widget.id}', value);

Now this special keyword can be used to know if it is a bool value or not

List<bool> prefList = [];
var sharedPreferences = await SharedPreferences.getInstance();
Set<String> keys = sharedPreferences.getKeys();

for(int i=0; i<keys.length ; i++){
  if(key.elementAt(i).contains('bool')){
    bool value = sharedPreferences.getBool(keys.elementAt(i));
    prefList.add(value);
  }
}

Edit:- You have to use the code inside a function and return you string Example:-

Future<List<bool>> getBoolList() async{
  List<bool> prefList = [];
  var sharedPreferences = await SharedPreferences.getInstance();
  Set<String> keys = sharedPreferences.getKeys();

  for(int i=0; i<keys.length ; i++){
    bool value = sharedPreferences.getBool(keys.elementAt(i));
    prefList.add(value);
  }
  
  return prefList;
}

Then call this function

List<bool> list = await getBoolList();

Note :- In simple words, only asynchronous functions will allow you to use await keyword inside them, and if they return something, that will be Future.

Arpit Awasthi
  • 493
  • 2
  • 8
  • where exactly should I insert this? I have some errors when I insert the code for example at await, for and key – Marcel Oct 27 '20 at 03:23
  • no idea if that is correct but i did it and i get no more errors saves() async { List prefList = []; var sharedPreferences = await SharedPreferences.getInstance(); Set keys = sharedPreferences.getKeys(); for (int i = 0; i < keys.length; i++) { bool value = sharedPreferences.getBool(keys.elementAt(i)); prefList.add(value); } } but how i can load now if this is right the bools to a list? – Marcel Oct 27 '20 at 04:11
  • @Marcel this code has to be written inside a function. The function can return List and the function has to be async. You can also have global variable of type List, your choice – Arpit Awasthi Oct 27 '20 at 20:09
  • code error Undefined name 'key'. and I don't understand how I get that into a ListView – Marcel Oct 27 '20 at 21:06
  • 1
    @Marcel bool value = sharedPreferences.getBool(keys.elementAt(i)); Please correct this line. It had 'key' but the variable is 'keys'. It will remove the error. How to load this list in ListView, please ask a separate question for this. – Arpit Awasthi Oct 27 '20 at 21:48