【问题标题】:Flutter: How to solve bug with dropdown.dart 'Failed assertion: lines 560' error?Flutter:如何解决 dropdown.dart 'Failed assertion: lines 560' 错误的错误?
【发布时间】:2019-11-18 12:45:10
【问题描述】:

我想将DropdownButton 移动到Step 类并在Stepper 中使用它,但我在下面收到此错误:

package:flutter/src/material/dropdown.dart':断言失败:行 560 位置 15: '项目 == 空 ||项目.isEmpty ||值 == 空 || items.where((DropdownMenuItem item) => item.value == value).length == 1':不正确。

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

TextStyle itemTextStyle = TextStyle(fontSize: 14);
TextStyle titleTextStyle = TextStyle(fontSize: 20);

class OrderStep {
  var index, value, title, hint;
  final mapElements;
  bool isActive = true;

  Step step;

  OrderStep(@required this.value, @required this.index, @required this.title,
      @required this.mapElements,
      {this.hint = ''})
      : assert(value != null),
        assert(index != null),
        assert(title != null) {
    setStep();
  }

  setStep() {
    step = Step(
        title: Text(
          title,
          style: titleTextStyle,
        ),
        isActive: isActive,
        content: OrderDropDownButton(this.value, this.mapElements));
  }
}

class OrderDropDownButton extends StatefulWidget {
  var value;
  var mapElements;

  OrderDropDownButton(this.value, this.mapElements) : assert(value != null), assert(mapElements != null);
  @override
  OrderDropDownButtonState createState() => OrderDropDownButtonState();
}


class OrderDropDownButtonState extends State<OrderDropDownButton> {
  List<DropdownMenuItem> items = [];
  DropdownButton ddButton = DropdownButton(items: null, onChanged: null);

  addDropDownItem(String text, String value) {
      items.add(DropdownMenuItem(
          child: Text(
            text,
            style: itemTextStyle,
          ),
          value: value));
  }

  buildDropDownItems() {
    widget.mapElements.forEach((t, v) => {addDropDownItem(t, v)});
  }

  setDropdownButton() {
    ddButton = DropdownButton(
        value: widget.value,
        isDense: true,
        isExpanded: true,
        hint: Text("Wybierz danie"),
        items: [
          DropdownMenuItem(
            child: Text("Subway1"),
            value: "Subway1",
          ),
          DropdownMenuItem(
            child: Text("Subway2"),
            value: "Subway2",
          ),
          DropdownMenuItem(
            child: Text("Subway3"),
            value: "Subway3",
          ),
          DropdownMenuItem(
            child: Text("Subway4"),
            value: "Subway4",
          ),
          DropdownMenuItem(
            child: Text("Subway4"),
            value: "Subway5",
          )
        ],
        onChanged: (newValue) {
          setState(() {
            widget.value = newValue;
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    setDropdownButton();
    return ddButton;
  }
}

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow。不要用 items: null 实例化你的 DropdownButton 。
  • 确保您用于实例化此下拉列表的值与任何项目的值相匹配。例如。 “地铁 5 号”
  • @Ryosuke ty 回答 :)
  • @vanowikv 很高兴为您提供帮助..

标签: dart flutter dropdown


【解决方案1】:

请确保所选值包含在项目中。 在您的情况下,widget.value 未使用列表中的任何值进行初始化。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2021-04-09
    • 2019-12-07
    • 2019-11-12
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多