I'm trying to set a list of string in the shared preferences but for unknown reason, it is throwing up the following error
type 'bool' is not a subtype of type 'List' in type cast
here is my code
List selectedItems = [];
setList() async {
var prefs = await SharedPreferences.getInstance();
List<String> itemList = selectedItems.map((e) => e.toString()).toList(); // converted int list to string
List<String> selectedItemsList = await prefs.setStringList('selectedItems', itemList) as List<String>; // error
}
I'm not assigning the bool type value to any variable, so why is this error occurring?
CodePudding user response:
await prefs.setString returns a bool thats why you are facing the error. As reference see https://pub.dev/documentation/shared_preferences/latest/shared_preferences/SharedPreferences/setString.html
What you probably wanted to do is use getStringList as so:
List<String> selectedItemsList = await prefs.getStringList('selectedItems') as List<String>;
or if you really wanted to save the list the line would be:
await prefs.setStringList('selectedItems', itemList) as List<String>;
CodePudding user response:
If you take a look at the docs, you'll see that setStringList
returns Future<bool>
.