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

问题描述 投票:0回答:1

我在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为空。谢谢您的时间。

flutter dart
1个回答
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);
            },
          ),
© www.soinside.com 2019 - 2024. All rights reserved.