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.