How to use TextEditingController in DropdownButton in Flutter?

Viewed 7279

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;
              });
            },

          ),

        ),
      ],
    ),


  ],
) ,
),
1 Answers
Related