Flutter中如何从下拉搜索包中获取当前选中的item

How can we get the current selected item from the Dropdown Search Package in Flutter

我正在使用这个包 https://pub.dev/packages/dropdown_search

我在我制作的表单中有一个下拉菜单,当用户选择一个项目时,我想获取选择的值并将其分配给一个变量。我读过一些文档,但在获取价值方面很难理解。据我了解,SelectedItem 属性 仅用于设置起始选择。如何从下拉列表中获取所选项目?

 DropdownSearch<String>(
                  mode: Mode.MENU,
                  showSearchBox: false,
                  showSelectedItems: true,
                  items: const [
                    'Item 1',
                    'Item 2',
                    'Item 3',
                  ],
                  dropdownSearchDecoration: InputDecoration(
                    labelText: 'Items',
                  ),
                ),

在我上面的代码中,我不明白我应该如何获取所选项目,我正在将所选项目上传到 Firestore。

有一个onChanged property for DropDownSearch。您可以使用它来获取所选项目。 例如:

    DropdownSearch<UserModel>(
      label: "Name",
      onFind: (String filter) => getData(filter),
      itemAsString: (UserModel u) => u.userAsStringById(),
      onChanged: (UserModel data) {     <----- THIS ONE
         //Do something with this data
         },
    )

我给你加了一个例子,请查收

 DropdownSearch<String>(
                  mode: Mode.MENU,
                  showSearchBox: false,
                  showSelectedItems: true,
                  items: const [
                    'Item 1',
                    'Item 2',
                    'Item 3',
                  ],
                  dropdownSearchDecoration: InputDecoration(
                    labelText: 'Items',
                  ),
                  onChanged: (String selectedValue) {
                    yourVariable = selectedValue; // you should change this line according to your own
                  },
                ),