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

3

7

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.

Bioclimatology answered 1/4, 2022 at 17:10 Comment(0)
A
3

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(),
              ),
            ])

          ],
        ),

    );
  }

}
Audiology answered 1/4, 2022 at 17:24 Comment(1)
You can also pass null on DropdownButton's value, it can take nullAgnomen
A
13

You are missing value on DropdownMenuItem.

.map((_value) => DropdownMenuItem<String>(
        value: _value, // this
        child: Text(
          _value,
        )))

Also make sure to use Scaffold on home.

Agnomen answered 1/4, 2022 at 17:19 Comment(0)
A
3

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(),
              ),
            ])

          ],
        ),

    );
  }

}
Audiology answered 1/4, 2022 at 17:24 Comment(1)
You can also pass null on DropdownButton's value, it can take nullAgnomen
P
3

please add the VALUE field in both DropdownMenuItem and DropdownButton to prevent error

Promotion answered 15/12, 2022 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.