Raffaele Rossi
Raffaele Rossi

Reputation: 3127

Flutter dropdown cannot change text with provider

I have this class that represents the currently selected item:

class DropdownText with ChangeNotifier {

  String _text = "";

  String get text => _text;

  void setText(String value) {
    _text = value;
    notifyListeners();
  }

}

And this is the code that I use to try to show a dropdown button:

class DropDown extends StatelessWidget {

  final List<DropdownMenuItem<String>> menu = [
    DropdownMenuItem<String>(
      value: "A",
      child: Text("A"),
    ),
    DropdownMenuItem<String>(
      value: "B",
      child: Text("B"),
    )
  ];

   DropDown();

  @override
  Widget build(BuildContext context) {
    return FormField<String>(
      builder: (FormFieldState<String> formState) {
        return Consumer<DropdownText>(
          builder: (context, dropdown, _) {
            return InputDecorator(
              decoration: InputDecoration(
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10)
                  ),
                prefixIcon: const Icon(Icons.fastfood),
              ),
              child: DropdownButtonFormField<String>(
                items: menu,
                onChanged: (value) {
                  dropdown.setText(value);
                },
                value: dropdown.text,
              ),
            );
          },
        );
      },
    );
  }

}

The error I get is

Failed assertion: line 1411 pos 15: 'items == null || items.isEmpty || value == null || I/flutter ( 7719):
items.where((DropdownMenuItem item) { I/flutter ( 7719):
return item.value == value; I/flutter ( 7719): }).length == 1'

But where am I failing? I have explicitly put every generic type. Might it be a problem of Consumer<T> from the provider package?

Also I see no duplicates and no null items in the dropdown.

Upvotes: 2

Views: 1064

Answers (2)

A.K.J.94
A.K.J.94

Reputation: 552

if the current value assigned is not from the list of values we may get this type error , to fix cross check the current value if it is different assign default null will save code from break,

eg: currentValue: (apiDataValue.value?.isEmpty??true)?null:apiDataValue.value,

Upvotes: 0

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12673

When you read this part of the error

 items.where((DropdownMenuItem<T> item) {return item.value == value;}).length == 1'

You'd notice that if the length is 0 the error will still be flagged

Basically, the initial value of the dropdown must be a value of one DropDownItem. And because your initial value is "" and there is no DropDownItem with "" as its value, it won't work

 String _text = "";

The solution

Set the initial value of _text in your DropDownText to the first value of your items (DropDownItems)

class DropdownText with ChangeNotifier {

  String _text = "A";

  String get text => _text;

  void setText(String value) {
    _text = value;
    notifyListeners();
  }

}

Upvotes: 3

Related Questions