0

Mi problema es:

  1. Lleno un DropDownList con registros obtenidos de un API
  2. Luego selecciono uno de los items y este evento llena un segundo DropDownList, que al igual que el primero recibe datos de una API
  3. Cuando vuelvo a hacer click en el primer DropDownList aparece el error:

Código del primer DropDownList

 Widget _regionesContainer() {
    return new Container(
      child: new DropdownButton<String>(
        items: dataRegion.map((item) {
          return new DropdownMenuItem<String>(
            value: item['ID_REGION'].toString(),
            child: new Text(item['NOMBRE_REGION'],
            style: new TextStyle(fontSize: 13.0)
          ),
          );
        }).toList(),
        hint: Text("Seleccione región"),
        onChanged: (String newValReg) {
          setState(() {
            _mySelectionReg = newValReg;
            capturaComuna(newValReg);
            Fluttertoast.showToast(msg: _mySelectionReg,
              fontSize: 18.0,
            );
          });
        },
        value: _mySelectionReg,
      ),
      margin: EdgeInsets.only(bottom: 40.0)
    );
  }

Este es el código del segundo DropDownList

    Widget _comunasContainer() {
    return new Container(
        child: new DropdownButton<String>(
          items: dataComuna.map((item) {
            return new DropdownMenuItem<String>(
              value: item['ID_COMUNA'].toString(),
              child: new Text(item['NOMBRE_COMUNA'],
                  style: new TextStyle(fontSize: 13.0)),
            );
          }).toList(),
          hint: Text("Seleccione comuna"),
          onChanged: (newValCom) {
            _mySelectionCom = newValCom;
            setState(() {
            });
          },
          value: _mySelectionCom,
        ),
        margin: EdgeInsets.only(bottom: 45.0)
    );
  }

Este es el código de los métodos future:

String _mySelectionReg;
  String _mySelectionCom;

  String urlRegion = "http://www.aquimaestros.cl/am/Region/Read.php";

  List dataRegion = List(); //Lista de regiones
  List dataComuna = List(); //Lista de comunas

  Future<String> getSWData() async {
    var res = await http
        .get(Uri.encodeFull(urlRegion), headers: {"Accept": "application/json"});
    var resBody = json.decode(res.body);
    setState(() {
      dataRegion = resBody;
    });
    print(resBody);
    return "Sucess";
  }

  Future<String> capturaComuna(String _id) async {
    String urlComuna = "http://www.aquimaestros.cl/am/Comuna/ReadxRegion.php?id_region=${_id}";
    var res = await http
        .get(Uri.encodeFull(urlComuna), headers: {"Accept": "application/json"});
    var resBody = json.decode(res.body);
    setState(() {
      dataComuna = resBody;
    });
    print(resBody);
    return "Sucess";
  }

El error se da al dar click por segunda vez en el DropDownList del widget _regionesContainer, este es el error:

introducir la descripción de la imagen aquí

1 respuesta 1

0

Esto sucede sólo si ya seleccionaste un elemento en el segundo DropdownButton. Al seleccionar un item estás actualizando el valor del atributo _mySelectionCom con el valor seleccionado. Cuando seleccionas otro item en el primer DropDownButton necesitas limpiar el atributo _mySelectionCom para que el segundo DropdownButton no intente buscar _mySelectionCom dentro de sus items

Limpiar el atributo _mySelectionCom:

 Widget _regionesContainer() {
    return new Container(
      child: new DropdownButton<String>(
        items: dataRegion.map((item) {
          return new DropdownMenuItem<String>(
            value: item['ID_REGION'].toString(),
            child: new Text(item['NOMBRE_REGION'],
            style: new TextStyle(fontSize: 13.0)
          ),
          );
        }).toList(),
        hint: Text("Seleccione región"),
        onChanged: (String newValReg) {
          setState(() {
            _mySelectionReg = newValReg;
            _mySelectionCom = null; // <====== Agregar esta línea
            capturaComuna(newValReg);
            Fluttertoast.showToast(msg: _mySelectionReg,
              fontSize: 18.0,
            );
          });
        },
        value: _mySelectionReg,
      ),
      margin: EdgeInsets.only(bottom: 40.0)
    );
  }
3

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.