0
votes

I want to move DropdownButton to Step class and letter use it in Stepper but I'm getting this error below:

package:flutter/src/material/dropdown.dart': Failed assertion: line 560 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.

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;
  }
}
1
Hi, welcome to Stack Overflow. Do not instantiate your DropdownButton with items: null .Rubens Melo
make sure that the value you are using to instantiate this Dropdown matches the value from any of items. For ex. "Subway5"Ryosuke
@Ryosuke ty for answer :)vanowikv13
@vanowikv glad to be of help..Ryosuke

1 Answers

0
votes

Please ensure that the selected value is included in items. In your case, the widget.value is not initialized with any value in the list.

I hope this help