Dropdownbutton does not work when searching for the Db async Future using the Widget: Future Builder Flutter

Asked

Viewed 83 times

-2

Exception that appears:

Exception Caught by widgets library

The following was thrown building Futurebuilder(Dirty, state: _Futurebuilderstate#74679): type '(City) => Dropdownmenuitem' is not a subtype of type '(Map) => Dropdownmenuitem' of 'f'

I use the Stateful Class:

class Sincronismo extends StatefulWidget {
  @override
  _SincronismoState createState() => _SincronismoState();
}

class _SincronismoState extends State<Sincronismo> {
 // return Users.fromJson(jsonresponse[0]);

**Aqui pego os dados da Classe DB:**

  getCidade( ) async {
    DBAvaliacoes db = new DBAvaliacoes();
    List city = await db.getCitys();
    return city;

  }

  Cidade selectedCountry;

  Future _future;

  @override
  void initState() {
    _future = getCidade();
    super.initState();



 }
  @override
  Widget build(BuildContext context) {

    return  FutureBuilder (
        future: _future ,
        builder: (context, snapshot){
          if(snapshot.hasError)
            return Text(snapshot.error);

          if (snapshot.hasData) {
            return  DropdownButtonFormField(
              decoration: new InputDecoration(icon: Icon(Icons.language)), //, color: Colors.white10
              value: selectedCountry,

The exception is when you get here:

items: snapshot.data.map<DropdownMenuItem<Cidade>>((Cidade city) {


                return  DropdownMenuItem<Cidade>(

                  value: city,
                  child: Text(city.descricao_cidade, style: TextStyle(color: Color.fromRGBO(58, 66, 46, .9))),

                );
              })
                  .toList(),

              onChanged: (Cidade newValue) {
                setState(() => selectedCountry = newValue);
                // selectedCountry = newValue;
                print(newValue.id);
                print(newValue.descricao_cidade);
              },
            );


          }

          else{
            return CircularProgressIndicator();
          }
        }

      /**********************/
    );

  }}
  • Within the db. getCitys() Do you do some treatment to bring the City Object? If you do not do this treatment, it is returning a Map and not the model

1 answer

0

I got it, it was because Future sends a MAP, I converted the getCidades to a City Object Map:

getCidadess() async{

    DBAvaliacoes db = new DBAvaliacoes();
    List cidades = await db.getCitys();
    List<Cidade> listaTemporaria = List<Cidade>();
    for(int i = 0; i< cidades.length; i++){
      Cidade c = Cidade.fromMap(cidades[i]);
      listaTemporaria.add(c);
    }
    setState(() {
      _listaCidades = listaTemporaria;
      _dropdownMenuItems = buildDropdownMenuItems(_listaCidades);
      _selectedCidade = _dropdownMenuItems[0].value;
    });
    listaTemporaria = null;
    print(_listaCidades.toString());

  }

Thank you Matheus Ribeiro

Browser other questions tagged

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