(GETX) Class 'String' has no instance getter 'Description'." Tried Calling: Description

Asked

Viewed 16 times

0

I’m trying to do 2 Dropdownbutton, the first that when selecting the UF the second list of its cities however I’m not getting at all, I’m making the request and adding the cities of this request in the list of cities but using it only of error, it seems that she is not adding I could not identify very well the error, but I have tried several things and nothing... Does anyone know any solution?

Controller:

  List cidades = [].obs;
  var selected = "UF".obs;
  var selectedCidades = "Selecione o Estado".obs;

  void setSelectedCity(var value){
    selectedCidades.value = value;
  }

  void setSelected(var value){
    selected.value = value;
  }
    
getByUf(String uf) async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        final id_distribuidor = prefs.getInt('id_distribuidor');
        final token = prefs.getString('access_token');
        try {
          final response = await Dio().get(
              '####################################################',
              options: Options(
                headers: {'Authorization': "Bearer ${token}"},
              ));
          print(response.data);
          final list = response.data;
    
          cidades.clear();
    
          for (var item in list) {
            CidadesModel cidade = new CidadesModel.fromJson(item);
            cidades.add(cidade.descricao);
          }
          return this.cidades;
        } on DioError catch (e) {
          if (e?.response?.statusCode == 401) {
            Get.snackbar(
                'Sem permissão', 'Tente logar novamente!');
          } else if (e?.response?.statusCode == 500) {
            print(e);
            Get.snackbar('Erro', 'Ocorreu um erro inesperado');
          }
        }
      }

Model:

class CidadesModel {
  int idCidade;
  String descricao;
  String uf;
  int codigoIbge;
  String ddd;

  CidadesModel(
      {this.idCidade, this.descricao, this.uf, this.codigoIbge, this.ddd});

  CidadesModel.fromJson(Map<String, dynamic> json) {
    idCidade = json['id_cidade'];
    descricao = json['descricao'];
    uf = json['uf'];
    codigoIbge = json['codigo_ibge'];
    ddd = json['ddd'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id_cidade'] = this.idCidade;
    data['descricao'] = this.descricao;
    data['uf'] = this.uf;
    data['codigo_ibge'] = this.codigoIbge;
    data['ddd'] = this.ddd;
    return data;
  }
}

Dropdown:

              Row(
                children: [
                  Obx(
                    () => DropdownButtonHideUnderline(
                    child: ButtonTheme(
                      child: DropdownButton<String>(
                        value: '${navController.selected}',
                        hint: Text('UF'),
                        onChanged: (String newValue) {
                          navController.setSelected(newValue);
                          navController.getByUf(newValue);
                        },
                        items: Estados.listaEstadosSigla.map((item) {
                              return new DropdownMenuItem(
                                child: new Text(item),
                                value: item,
                              );
                            }).toList(),
                      ),
                    ),
                  ),
                  ),
                ],
              ),
              Row(
                children: [
                  Obx(
                    () => DropdownButtonHideUnderline(
                    child: ButtonTheme(
                      child: DropdownButton<String>(
                        value: '${navController.selectedCidades}',
                        hint: Text('Cidade'),
                        onChanged: (String newValue) {
                          navController.setSelectedCity(newValue);
                        },
                        items: navController.cidades.map((item) {
                              return new DropdownMenuItem(
                                child: new Text(item.descricao),
                                value: item.idCidade.toString(),
                              );
                            }).toList(),
                      ),
                    ),
                  ),
                  ),
                ],
              ),
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.