首页 > 解决方案 > Flutter:在MultiSelect中选定项目后未触发onSaved事件

问题描述

我正在使用官方的 Flutter Multiselect - flutter_multiselect: ^0.5.1 一切正常,但是在选择/取消选择并单击 MultiSelect 小部件中的保存按钮后未触发 onSaved() 事件。我只想在控制台中打印选中/取消选中的项目。

注意:我也试图通过 change() 事件,它只有在我们选择选项时才有效,在取消选择时无效。

帮我解决这个问题

示例代码:

Container(
  decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(5.0),
  ),
  alignment: Alignment.center,
  child: new MultiSelect(
    maxLength: 1,
    maxLengthText: "",
    autovalidate: false,
    dataSource: countries,
    textField: 'country_name',
    valueField: 'country_id',
    hintText: "Select Your Country",
    initialValue: countryID,
    value: countryID,
    required: true,
    validator: (value) {
      if (value == null) {
        return 'Please Select Country';
      }
    },
    filterable: true,
    change: (values) {
      if (values != null) {
        setState(() {
          countryID = values.cast<String>();
          getStatesByCountry();
        });
      }
      //this event emitted while selecting option from multiselect
      //not works on deselecting option from multiselect
    },
    onSaved: (value) {
      debugPrint("on save event");
      print(value);
      //always not emitting
    },
    
  ),
)

标签: flutterflutter-layoutflutter-dependencies

解决方案


我不知道它是正确的方法,但在我的情况下,只是清理了一个构建并重新安装了 packge 会在删除多选中的项目时发出 onchange 事件,没有其他方法对我有用


推荐阅读