如何将从颤动的多选小部件中选择的值保存到列表中

时间:2019-11-18 17:01:26

标签: flutter dart

我在flutter项目中使用了这个flutter包“ multiselect”来从下拉列表中选择多个数据。

List _fines;

MultiSelect(
            autovalidate: false,
            titleText: "Fine list",
            validator: (value) {
              if (value == null) {
                return 'Please select one or more option(s)';
              }
            },
            errorText: 'Please select one or more option(s)',
            dataSource: [
              {"display": "Fine No 1", "index": 1, "value": 5000},
              {"display": "Fine no 2", "index": 2, "value": 2500},
              {"display": "Fine no 3", "index": 3, "value": 1000},
              {"display": "Fine no 4", "index": 4, "value": 500}
            ],
            textField: 'display',
            valueField: 'index',
            filterable: true,
            required: true,
            value: null,
            onSaved: (value) {
              setState(() {
                  _fines = value;
                });

            })

我无法将数据保存到列表中。在on保存后,它说_fines list为空。感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

您可以使用change回调函数作为MultiSelect小部件的参数来获取最后选择的值

List<int> selectedValues = [];

MultiSelect(
            autovalidate: false,
            titleText: "title",
            validator: (value) {
              if (value == null) {
                return 'Please select one or more option(s)';
              }
            },
            errorText: 'Please select one or more option(s)',
            dataSource: [
              {
                "display": "Australia",
                "value": 1,
              },
              {
                "display": "Canada",
                "value": 2,
              },
              {
                "display": "India",
                "value": 3,
              },
              {
                "display": "United States",
                "value": 4,
              }
            ],
            textField: 'display',
            valueField: 'value',
            filterable: true,
            required: true,
            value: null,
            change: (values){
              selectedValues = values;
              print(selectedValues);
            },
          ),