I am new to programming with flutter, and a problem has arisen that, after reading a lot in this forum, I have not managed to solve.
I have two classes, one Provinces and another cities, each of them are obtained through an API query.
First I get the provinces with a webservices. and I mount with a futurebuilder the dropdown with the provinces.
Once the province is selected, and through another web service to which we pass the province, we obtain the cities through a different web service.
and with a FutureBuilder I create the dropdown with the cities of the requested province.
Everything seems correct .... but once I have the list of cities, if we select a province other than the one selected, I get the following error:
=============================================================================================
There should be exactly one item with [DropdownButton]'s value: Instance of 'Municipio'.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart':
Failed assertion: line 803 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) {
return item.value == value; }).length == 1'
=============================================================================================
@override
void initState() {
_provincias = provinciasProvider.getProvincias();
super.initState();
}
Widget _dropDownProvincias() {
return FutureBuilder(
future: _provincias,
builder: (context, snapshot) {
if (snapshot.hasError) return Text(snapshot.error);
if (snapshot.hasData) {
return DropdownButtonFormField(
isExpanded: false,
isDense: true,
decoration: new InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.greenAccent, width: 5.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.teal, width: 1.0),
),
labelText: 'Seleccione Provincia',
labelStyle: TextStyle(color: Colors.black),
//icon: Icon(Icons.language)
), //, color: Colors.white10
items: snapshot.data
.map<DropdownMenuItem<Provincia>>((Provincia provincia) {
return DropdownMenuItem<Provincia>(
value: provincia,
child: Text(provincia.nombreProvincia,
style: TextStyle(color: Color.fromRGBO(58, 66, 46, .9))),
);
}).toList(),
onChanged: (Provincia newValue) {
setState(() {
selectedProvincia = newValue;
codigoProvinciaSeleccionada = newValue.codigoProvincia;
nombreProvinciaSeleccionada = newValue.nombreProvincia;
_municipios = municipiosProvider
.getMunicipios(codigoProvinciaSeleccionada);
});
print(codigoProvinciaSeleccionada);
print(newValue.nombreProvincia);
},
);
}
return Container();
});
Widget _dropDownPoblacion(String codigoProvinciaSeleccionada) {
return FutureBuilder(
future: _municipios,
builder: (context, snapshot) {
if (snapshot.hasError) return Text(snapshot.error);
if (snapshot.hasData) {
return DropdownButtonFormField(
isExpanded: true,
isDense: true,
decoration: new InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.greenAccent, width: 5.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.teal, width: 1.0),
),
labelText: 'Seleccione Localidad',
labelStyle: TextStyle(color: Colors.black),
//icon: Icon(Icons.language)
), //, color: Colors.white10
items: snapshot.data
.map<DropdownMenuItem<Municipio>>((Municipio municipio) {
return DropdownMenuItem<Municipio>(
value: municipio,
child: Text(municipio.nombreMunicipio,
style: TextStyle(color: Color.fromRGBO(58, 66, 46, .9))),
);
}).toList(),
onChanged: (Municipio newValue) {
setState(() => selectedMunicipio = newValue);
codigomunicipioSeleccionado = newValue.codigoMunicipio;
nombreMunicipioSeleccionado = newValue.nombreMunicipio;
print(codigomunicipioSeleccionado);
print(newValue.nombreMunicipio);
},
);
}
return Container();
});
Thank for us help¡¡¡
The solution for me was to change DropdownButtonFormField to DropdownButton.
From there everything worked.