简体   繁体   English

如何添加列表<String> CheckboxListTile 值?

[英]How to ADD a List<String> to CheckboxListTile values?

On this example im trying to add to the CheckboxListTile the values i have saved on my List.在这个例子中,我试图将我保存在我的列表中的值添加到 CheckboxListTile。 But for somereason im stuck here and i cant figure it out how to solve this problem.但出于某种原因,我被困在这里,我无法弄清楚如何解决这个问题。

Global List全局列表

List<String> _lstNomeData = new List<String>();

Values Map价值观地图

Map<String, bool> values = {
    '$_lstNomeData[index]': false,
  };

Get selected checkbox values获取选中的复选框值

  var tmpArray = [];

  getCheckboxItems() {
    values.forEach((key, value) {
      if (value == true) {
        tmpArray.add(key);
      }
    });

    print(tmpArray);
    tmpArray.clear();
  }

Body身体

body: Column(
                children: <Widget>[
                  Expanded(
                    child: ListView(
                      children: values.keys.map((String key) {
                        return new CheckboxListTile(
                          title: new Text(key),
                          value: values[key],
                          activeColor: Colors.blue,
                          checkColor: Colors.white,
                          onChanged: (bool value) {
                            setState(() {
                              values[key] = value;
                            });
                          },
                        );
                      }).toList(),
                    ),
                  ),
                ],
              )

Error Print of the error displayed错误打印显示的错误

You are on the right track.你走在正确的轨道上。 You can do two things here:你可以在这里做两件事:

  1. Prefill the map with false for every key (what you are trying to do)为每个键用false预填充地图(您正在尝试做什么)
  2. Assume that if the map does not have a key, the answer is false (default)假设如果map没有key,则答案为false (默认)

Second approach is probably even better because by prefilling the map with false you could not distinct between false being actually answered by the user or if it was set by default.第二种方法可能更好,因为通过用false预填充地图,您无法区分用户实际回答的false或默认设置。 If a key is not in the map you know that the user has not answered the question so far.如果地图中没有键,您就知道用户到目前为止还没有回答问题。 I will go and show how to work with the second approach:我将去展示如何使用第二种方法:

Keep your global list as it is:保持您的全局列表原样:

List<String> _lstNomeData = [];

Initialise the map (which represents the answers from the user for each question) with an empty Map:使用空 Map 初始化地图(代表用户对每个问题的答案):

Map<String, bool> answers = {};

Now correctly reference those answers in your Checkbox widget and make use of the onChanged property:现在在Checkbox小部件中正确引用这些答案并使用onChanged属性:

ListView(
  children: _lstNomeData.map((key) =>
    CheckboxListTile(
      title: Text(key),
      // Here we check if the answers map has this key and if it does, use the bool value, otherwise set it to false (user has not answered this so far)
      value: answers[key] ?? false,
      activeColor: Colors.blue,
      checkColor: Colors.white,
      onChanged: (answer) => 
        setState(() => answers[key] = answer)
    ),
  ).toList(),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM