简体   繁体   中英

Either zero or 2 or more [DropdownMenuItem]s were detected with the same value Failed assertion: line 882 pos 15: 'items == null || items.isEmpty

I tried the solutions provided on the same question, but each question has its own case, so in my case i am trying to create drop down of countries list and before use it in the Scaffold widget i want to save it to an instance of type DropdownButton(), here is the code:

 List<String> provinces = [
    'عمان',
    'إربد',
    'الزرقاء',
    'المفرق',
    'الطفيلة',
    'عجلون',
    'معان',
    'الكرك',
    'مادبا',
    'العقبة',
    'البلقاء',
    'جرش'
  ];


String dropDownValue = "";

final provincesField = DropdownButton<String>(
  value: dropDownValue,
  icon: Icon(Icons.keyboard_arrow_down),
  items: provinces.map((String items) {
    return DropdownMenuItem(value: items, child: Text(items));
  }).toList(),
  onChanged: (String? newValue) {
    setState(() {
      dropDownValue = newValue!;
    });
  },
);

Am not sure why its not accessing the list and consider it empty. Here is the full error:

The following assertion was thrown building RegistrationScreen(dirty, state: _RegistrationScreenState#b01ae):
There should be exactly one item with [DropdownButton]'s value: . 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
package:flutter/…/material/dropdown.dart:1
Failed assertion: line 882 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

The error:

There should be exactly one item with [DropdownButton]'s value: one. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

Indicates that in your list provinces it should contain the same value as your dropDownValue .

Which means, that your file should look like this:

String dropDownValue = "one";
  List<String> provinces = [
    "one", // <-- Notice we're adding the word "one" to the list since that's the value of `dropDownValue`
    'عمان',
    'إربد',
    'الزرقاء',
    'المفرق',
    'الطفيلة',
    'عجلون',
    'معان',
    'الكرك',
    'مادبا',
    'العقبة',
    'البلقاء',
    'جرش'
  ];

Here is a complete example:

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

  @override
  State<Test> createState() => _TestState();
}

class _TestState extends State<Test> {
  List<String> provinces = [
    "one", // <-- Notice wer're adding the word "one" to the list
    'عمان',
    'إربد',
    'الزرقاء',
    'المفرق',
    'الطفيلة',
    'عجلون',
    'معان',
    'الكرك',
    'مادبا',
    'العقبة',
    'البلقاء',
    'جرش'
  ];

  @override
  Widget build(BuildContext context) {

    // make sure this is this value is in your list
    String dropDownValue = "one";

    final provincesField = DropdownButton<String>(
      value: dropDownValue,
      icon: Icon(Icons.keyboard_arrow_down),
      items: provinces.map((String items) {
        return DropdownMenuItem(value: items, child: Text(items));
      }).toList(),
      onChanged: (String? newValue) {
        setState(() {
          dropDownValue = newValue!;
        });
      },
    );

    return Container();
  }
}

Try and replace the code instead of String dropDownValue.

var dropDownValue;

And items should have a value other than null.

in constructor (){
   dropDownValue = provinces.first;
}

make sure a value is in your list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM