I get this error from the DropdownButton Widget.
`There should be exactly one item with [DropdownButton]'s value: Seleccionar.
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 890 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'`
Here's the code
class _NewCauseMenuState extends State<NewCauseMenu> {
final db = FirebaseDatabase.instance;
var dropdownValue;
var setDefaultSelection = true;
@override
Widget build(BuildContext context) {
final myRef = db.ref();
return WillPopScope(
onWillPop: () {
return _onWillPopScope();
},
child: SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Menú Nueva Causa ${widget.area}'),
),
body: Center(
child: Column(
children: [
const SizedBox(
height: 25,
),
StreamBuilder(
stream: myRef.child(widget.area.toString()).onValue,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
final lista = Map<String, dynamic>.from(
(snapshot.data)!.snapshot.value as Map);
return DropdownButton(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
isExpanded: false,
elevation: 16,
underline: Container(
height: 2,
color: Colors.blueAccent,
),
items: lista.keys.map(((e) {
return DropdownMenuItem(
value: e.toString(), child: Text(e.toString()));
})).toList(),
onChanged: (value) {
setState(() {
dropdownValue = value;
setDefaultSelection = false;
});
},
);
}
if (setDefaultSelection) {
dropdownValue = 'Seleccionar';
}
return Container();
},
),
],
),
),
)),
);
}
I was searching for a solution and I found that the value from the DropdownButton must be unique or that the value doesn't pass to the DropdownMenuItem, but I don't see clearly where I'm wrong. Help me, please.
I tried use a if sentence where a variable defined with boolean true pass to false where the value changed on the DropdownButton, so the value 'Seleccionar' doesn't pass to all the values from the MenuItem. But it didn't work.
When I remove the value property from the DropdownButton, occurs this:
This might be because the stream is returning duplicate values.
Change this part of the code,
items: lista.keys.map(((e) {
return DropdownMenuItem(
value: e.toString(), child: Text(e.toString()));
})).toList().toSet().toList(),
// notice toSet()
This will remove duplicate items from the list.
Or, the error might be because of below reason.
The DropdownButton
Widget's value should be present in the DropdownMenu
items. Otherwise you will get this error.
In your case, Seleccionar is the value of dropdown. But Seleccionar is not there among the dropdown items.
if (setDefaultSelection) {
dropdownValue = 'Seleccionar';
}
The default value is not there in the DropdownMenu
items.
If you change default value to Obsolecencia, Producto caducado or Stock out the error will go away.