【问题标题】:dropdown_search Flutter not returning items when typing into search bar输入搜索栏时,dropdown_search Flutter 不返回项目
【发布时间】:2020-07-05 14:04:03
【问题描述】:

我使用的是 dropdown_search 版本 0.4.3 包,我可以在单击搜索栏时使用项目填充下拉列表,但是当我开始输入时它不会返回任何项目。 我正在从 Firebase 云存储中获取初始数据。 请看我下面的代码。

@override
  Widget _buildBody() {
    return OurContainer(
      child: Column(
        children: <Widget>[
          DropdownSearch(
            showSearchBox: true,
            isFilteredOnline: true,
            onFind: (String filter) => getData(filter),
            popupItemBuilder: _customPopupItemBuilderExample,
            dropdownBuilder: _customDropDownExample,
          ),
        ],
      ),
    );
  }

  Widget _customDropDownExample(
      BuildContext context, MainCategoryModel item, String itemDesignation) {
    return Container(
      child: (item?.name == null)
          ? ListTile(
              contentPadding: EdgeInsets.all(0),
              title: Text("No item selected"),
            )
          : ListTile(
              contentPadding: EdgeInsets.all(0),
              title: Text(item.name),
            ),
    );
  }

  Widget _customPopupItemBuilderExample(
      BuildContext context, MainCategoryModel item, bool isSelected) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 8),
      decoration: !isSelected
          ? null
          : BoxDecoration(
              border: Border.all(color: Theme.of(context).primaryColor),
              borderRadius: BorderRadius.circular(5),
              color: Colors.white,
            ),
      child: ListTile(
        selected: isSelected,
        title: Text(item.name),
        subtitle: Text(item.name.toString()),
      ),
    );
  }

 



    Future<List<MainCategoryModel>> getData(filter) async {
    final tempList = <MainCategoryModel>[];
    if (filter.length > 0) {
      tempList.addAll(_mainCategoryList
          .where((element) => element.name.toLowerCase().contains(filter)));
      _mainCategoryList.addAll(tempList);
    } else {
      _mainCategoryList.clear();
      _mainCategoryList.addAll(await OurDatabase().getMainCategory());
    }

    return _mainCategoryList;
  }

【问题讨论】:

    标签: flutter dart flutter-packages


    【解决方案1】:

    您的getData 方法看起来不正确。假设您的 _mainCategoryList 是 Firebase 的列表,则以下 同步 方法应该可以工作:

    List<MainCategoryModel> getData(filter) {
      if (filter.length < 1) {
        return _mainCategoryList;
      }
      return _mainCategoryList.where((element) => element.name.toLowerCase().contains(filter)).toList();
    }
    

    此方法不会改变您的 _mainCategoryList,而是将其保留为缓存,以便搜索更快并减少您从 Firebase 读取的次数。如果这不能满足您的需要,请在此问题下方的 cmets 中告诉我,我很乐意提供帮助。

    【讨论】:

    • 嗨 Gregory,我不得不将方法更改为未来,因为 onfind 中的 getData 抛出错误返回类型不是未来。我更改了它以通过错误并能够在输入的第一个字母上返回一些数据,但是一旦我输入另一个字母它什么也没有返回。感谢您的帮助
    • 它没有返回任何内容,这意味着您的 getData 中的过滤器存在问题。您可以将 isFilteredOnline 更改为 false,一切都会好起来的(如果您没有在 getData 结果中加载所有数据,则不建议这样做)。
    猜你喜欢
    • 2022-08-16
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2023-01-24
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多