shakky
shakky

Reputation: 472

type 'bool' is not a subtype of type 'List<String>' in type cast - flutter/dart

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?

Upvotes: 0

Views: 1634

Answers (2)

Riwen
Riwen

Reputation: 5200

If you take a look at the docs, you'll see that setStringList returns Future<bool>.

Upvotes: 0

DEFL
DEFL

Reputation: 1052

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>;

Upvotes: 1

Related Questions