flutterdropdownflutter-widgetflutter-dropdownbutton

Failed assertion: line 1252 pos 12: 'widget.items!.where((DropdownMenuItem<T> item) => item.value == widget.value).length == 1': is not true


I am getting this error in the console when I am trying to use flutter DropdownButton Widget.

package:flutter/src/material/dropdown.dart': Failed assertion: line 1252 pos 12: 'widget.items!.where((DropdownMenuItem item) => item.value == widget.value).length == 1': is not true.

There is a long traceback... Here I am adding small code sample that will reproduce this error... Anyone can simply copy paste in main.dart file


// flutter import
 import 'package:flutter/material.dart';

void main() {
  runApp(const BugReportApp());
}

class BugReportApp extends StatefulWidget {
  const BugReportApp({Key? key}) : super(key: key);

  @override
  State<BugReportApp> createState() => _BugReportAppState();
}

class _BugReportAppState extends State<BugReportApp> {
  final TextEditingController _dropdownController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bug Report',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Flex(direction: Axis.vertical, children:[
                        DropdownButton<String>(
            value: _dropdownController.text == ""
                ? null
                : _dropdownController.text,
            items: ["hello, world", "how are you", "goodbye"]
                .map((_value) => DropdownMenuItem<String>(
                        child: Text(
                      _value,
                    )))
                .toList(),
            onChanged: (_value) {
              setState(() {
                _dropdownController.text = _value ?? _dropdownController.text;
              });
            },
          ),
      ],),
    );
  }
}

I was expecting dropown to work normally but, I don't know why it didn't.


Solution

  • Try this code, also added some explanation in the code:

    class _MyHomePageState extends State<MyHomePage> {
    
      final TextEditingController _dropdownController = TextEditingController();
    
      String? dropDownValue = 'hello, world'; // add one value as the defaul one which must exists in the dropdown value
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.white,
          body: Column(
              children: [
    
                Flex(direction: Axis.vertical, children:[
                  DropdownButton<String>(
                    value: dropDownValue, // this place should not have a controller but a variable
                    onChanged: (_value) {
                      setState(() {
                        dropDownValue = _value;
                      });
                    },
                    items: ["hello, world", "how are you", "goodbye"]
                        .map<DropdownMenuItem<String>>((String _value) => DropdownMenuItem<String>(
                        value: _value, // add this property an pass the _value to it
                        child: Text(_value,)
                    )).toList(),
                  ),
                ])
    
              ],
            ),
    
        );
      }
    
    }