DropdownButton cause null exception as i am passing string variable in value name parameter.
String diseases = "Select a disease";
DropdownButton<String>(
value: diseases,
items: model.diseases
.map((String value) {
return DropdownMenuItem<
String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (d) {
setState(() {
diseases = d;
print(
diseases.toString());
});
},
)
It works fine if I used first selected element from the array value: diseases, by value: model.diseases[0], and it also works fine if I hotreload after changing model.diseases[0] to "diseases" in value
════════ Exception Caught By widgets library
═══════════════════════════════════ The following assertion was thrown
building Builder(dirty): 'package:flutter/src/material/dropdown.dart':
Failed assertion: line 608 pos 15: 'items == null || items.isEmpty ||
value == null || items.where((DropdownMenuItem item) => item.value
== value).length == 1': is not true.
Either the assertion indicates an error in the framework itself, or we
should provide substantially more information in this error message to
help you determine and fix the underlying cause. In either case,
please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
Copyright License:
Author:「M.ArslanKhan」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/59914891/dropdownbutton-cause-null-item-exception