Yusuf
Yusuf

Reputation: 213

storing list items to shared preferences flutter

i have a list of items which has a default value as false and i am using a checkbox to change the selected item to true, then i am try to add the true key to a list in the shared preferences, but i am getting an error message of 'The argument type 'String' can't be assigned to the parameter type 'List' thank you all


class BallGames extends StatefulWidget {
  @override
  BallGamesState createState() => new BallGamesState();
}

class BallGamesState extends State {
  Map<String, bool> List = {
    'Bubble Football ⚽': false,
    'Futsal 🧿': false,
    'Beach Volleyball 🏐': false,
    'Volleyball 🏐': false,
    'Dodgeball 🏀': false,
    'Rugby 🏉': false,
    'American Footbal 🏈': false,
    'Korftbal 🥎': false,
    'Netbal ⚾': false,
  };

  



  getItems() {
    List.forEach((key, value) async {
      if (value == true) {
        
        SharedPreferences prefs = await SharedPreferences.getInstance();
      
        prefs.setStringList('selectedGames', key); //the 'key' is underline as the error value
        // the error message: 'The argument type 'String' can't be assigned to the parameter type 'List<String>''
      }
    });


Upvotes: 0

Views: 1435

Answers (1)

Jahidul Islam
Jahidul Islam

Reputation: 12565

you wanted to store the list but you sent string instead of List<String>.

List<String> holder_1;

  getItems() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List.forEach((key, value) {
      if (value == true) {
        holder_1.add(key);
      }
    });
    prefs.setStringList('selectedGames', holder_1);
   
}

Upvotes: 1

Related Questions