【问题标题】:How to apply filter on a dropdownbottom如何在下拉菜单上应用过滤器
【发布时间】:2019-10-29 10:14:18
【问题描述】:

我有以下填充 DropdownButton 的代码,但我在编辑时无法过滤内容。

我需要帮助,因为我无法理解错误以及如何纠正问题以制作过滤器。

我尝试了几个过滤器都没有成功。下面应用的过滤器有以下错误:

断言失败:第 609 行 pos 15: 'items == null || items.isEmpty ||
值 == 空 || items.where ((DropdownMenuItem item) =>
item.value == value) .length == 1 ': 不正确。

这个错误来自下面的过滤器:

if (widget.filialID != 0) {
  _select = FilialModel(key: widget.filialID);
}

我找不到问题的解决方案。请帮忙。谢谢。

import 'package:flutter/material.dart';
import 'package:onex_qa/data/database.dart';
import 'package:onex_qa/models/filial_model.dart';

class DropFilialDb extends StatefulWidget {
  int filialID;
  DropFilialDb({Key key, this.filialID}) : super(key: key);
  @override
  _DropFilialDbState createState() => _DropFilialDbState();
}

class _DropFilialDbState extends State<DropFilialDb> {
  final String _tipo = 'Filial';
  FilialModel _select;
  final _listDb = DatabaseDb().getAllFilialList();

  @override
  void initState() {
    super.initState();

    if (widget.filialID != 0) {
      _select = FilialModel(key: widget.filialID);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(4, 0, 2, 0),
      child: Theme(
        data: Theme.of(context).copyWith(
          canvasColor: Theme.of(context).dialogBackgroundColor,
        ),
        child: FutureBuilder<List<FilialModel>>(
          future: _listDb,
          builder: (BuildContext context,
              AsyncSnapshot<List<FilialModel>> snapshot) {
            if (!snapshot.hasData) return CircularProgressIndicator();
            return DropdownButton<FilialModel>(
              items: snapshot.data.map((FilialModel value) {
                return new DropdownMenuItem<FilialModel>(
                  value: value,
                  child: new Text(value.filial),
                );
              }).toList(),
              style: TextStyle(color: Theme.of(context).primaryColorDark),
              elevation: 3,
              isExpanded: true,
              hint: Text("Selecione ${_tipo}",
                  style: TextStyle(
                      color: Theme.of(context).primaryColorDark, fontSize: 17)),
              value: _select == null ? null : _select,
              onChanged: (FilialModel value) {
                if (value == null) {
                  _select = null;
                } else {
                  setState(() {
                    _select = value;
                    widget.filialID = _select.key;
                  });
                }
              },
            );
          },
        ),
      ),
    );
  }
}

【问题讨论】:

  • 请告诉我们FilialModel的实现,因为错误来自那里;否则我们将无法为您提供帮助。

标签: flutter


【解决方案1】:

我的逻辑假设是FilialModel 没有定义相等运算符,因此比较两个模型总是返回false。即使他们相同的模型。

尝试在 FilialModel 类中定义相等运算符。这可能会修复引发错误的断言的items.where 部分。

例如

class FilialModel /* extends ... */ {
  // ...,

  // Compares `key` property of current object (this) and the other object (obj)
  bool operator ==(obj) => obj is FilialModel && obj.key == this.key;

  @override
  int get hashCode => key.hashCode; // This has to be overridden too. You might want to define more complex logic here.
}

如果这有帮助,请告诉我。

【讨论】:

  • @MarceloLR 很高兴它成功了!如果您觉得有帮助,请接受和/或投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2012-12-05
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多