Flutter:如何使用dropdown.dart解决错误:断言失败:560行错误?

问题描述 投票:0回答:1

我想将DropdownButton移至Step类,并在Stepper中使用字母,但在下面出现此错误:

package:flutter / src / material / dropdown.dart':断言失败:行560 pos 15:“项目== null || items.isEmpty ||值== null ||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;
  }
}
dart flutter dropdown
1个回答
0
投票

请确保所选值包括在项目中。您的情况下,widget.value不会用列表中的任何值初始化。

我希望这个帮助

© www.soinside.com 2019 - 2024. All rights reserved.