【问题标题】:storing list items to shared preferences flutter将列表项存储到共享首选项颤动
【发布时间】:2021-12-10 08:13:00
【问题描述】:

我有一个默认值为 false 的项目列表,我正在使用复选框将所选项目更改为 true,然后我尝试将 true 键添加到共享首选项中的列表中,但我是收到“参数类型“字符串”无法分配给参数类型“列表”的错误消息 谢谢大家


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>''
      }
    });


【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您想存储该列表,但您发送了string 而不是List&lt;String&gt;。

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

    【讨论】:

    • 好的。多谢 。我现在试过了。但我得到了这个错误'参数类型'List'不能分配给参数类型'List'。 .... holder_1 是错误的下划线
    • var holder_1 = []; 更改为List&lt;String&gt; holder_1 = []; 否则,更改此行prefs.setStringList('selectedGames', holder_1 as List&lt;String&gt;);
    • 您好,我收到一条错误消息。列表不是一种类型。无法在初始化程序中访问实例成员“列表”。谢谢
    • 你能分享setStringList('selectedGames', holder_1);部分的代码
    • 列表 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); }
    猜你喜欢
    • 2020-05-29
    • 2021-08-16
    • 2020-04-02
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    相关资源
    最近更新 更多