Home > Back-end >  Either zero or 2 or more [DropdownMenuItem]s were detected with the same value Failed assertion: lin
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value Failed assertion: lin

Time:07-13

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'

CodePudding user response:

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

CodePudding user response:

Try and replace the code instead of String dropDownValue.

var dropDownValue;

And items should have a value other than null.

  • Related