How to use TextEditingController in DropdownButton in Flutter?
dropdown button doesn't have controller property you can check the documentation https://api.flutter.dev/flutter/material/DropdownButton-class.html If you want to use text editing controller, you can use this package dropdown_search
https://pub.dev/packages/dropdown_search it has searchBoxController
property which is for TextEditingController
canarcho
i am originally a core network engineer , eager to learn scripting in python, flask ,dart, c++ and java.
Updated on December 27, 2022Comments
-
canarcho over 1 year
I am having trouble to use DropdownButton and TextEditingController at the same time. I am trying to take the user form data in a screen with text entries, parsing the data i received and sending it through API call with http.post. Due to the application structure, i have to use controllers.
How should i define the variables ? Or how should i set the DropdownButton field?
I receive this error: 'There should be exactly one item with [DropdownButton]'s value'. Here is the full error;
Exception has occurred. _AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: line 839 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) { return item.value == value; }).length == 1': There should be exactly one item with [DropdownButton]'s value: TextEditingController#82061(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))). Either zero or 2 or more [DropdownMenuItem]s were detected with the same value)
Here i define the variable within a controller ;
class _TransferDataWidget extends State { // Getting value from TextField widget. TextEditingController shift= TextEditingController(); }
And to call an API, i am getting the value from Controller and parsing;
// Getting value from Controller String shift1 = shift.text;
And here is the form entry field ;
//String shift DropdownButton Container( width: 280, padding: EdgeInsets.all(10.0), margin: EdgeInsets.fromLTRB(0, 0, 0, 10), child: Column( children: <Widget>[ Text('Shift : ?'), Row( children: [ Flexible( fit: FlexFit.loose, child: DropdownButton( value: shift, items: [ DropdownMenuItem(child: Text("Gece"), value: 'gece',), DropdownMenuItem(child: Text("Gunduz"), value: 'gunduz',), ], onChanged: ( value) { setState(() { shift = value; }); }, ), ), ], ), ], ) , ),
-
canarcho over 3 yearsi ve used defining List and initalized the start value and solved with dropdown button. thanks for the recommendation, i am going to try in next project.
-
Ralph Shillington about 2 years@canarcho and update to your question with your solution would be helpful.