Home > OS >  Populate DropdownButton with sqflite in Flutter, when selecting value(OnChange) returns error
Populate DropdownButton with sqflite in Flutter, when selecting value(OnChange) returns error

Time:03-17

_AssertionError ('package:flutter/src/material/dropdown.dart': Failed assertion: 
line 894 pos 15: 'items == null || items.isEmpty || value == null || 
items.where((DropdownMenuItem<T> item) { return item.value == value; }).length == 1': 
There should be exactly one item with [DropdownButton]'s value: 

I'm getting this return when selecting value, I've tried it in several ways and I still can't. Whenever I select the value it returns this error. Can someone help me?

This is the block I am using to pull the categories, it only works from the third category onwards, before that it returns me this error that I mentioned like this.

FutureBuilder(
              future: Future.delayed(Duration(seconds: 1))
                  .then((value) => _daoCateg.findAll_categoria()),
              builder: (context, AsyncSnapshot snapshot) {
                if (snapshot.hasData && snapshot.data != null) {
                  final List<registro_categoria> _cadastro = snapshot.data;
                  return DropdownButton(
                    onChanged: (value) {
                      setState(() {
                        _selectedValue = value;
                      });
                    },
                    value: _selectedValue,
                    items: _cadastro.map((map) {
                      return DropdownMenuItem(
                        child: Text(map.nome_categoria.toString()),
                        value: map.nome_categoria.toString(),
                      );
                    }).toList(),
                    hint: Text('Selecione uma categoria'),
                  );
                } else {
                  return Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        CircularProgressIndicator(),
                        Text('Carregando favoritos'),
                      ],
                    ),
                  );
                }
              }),

This is the class I use to organize the database search:

 class registro_categoria {
  final String nome_categoria;
  final String cor_categoria;
  final String icone_categoria;

  registro_categoria(
      this.nome_categoria, this.cor_categoria, this.icone_categoria);
      
  bool operator ==(o) =>
      o is registro_categoria && o.nome_categoria == nome_categoria;
  int get hashCode => nome_categoria.hashCode;
}

As soon as the debug is executed, FutureBuilder brings me the data from the database. When I select for example item 3 or 4 of the DropdownButton it is assigned to 'value:' but when I select 1 and 2 it is not assigned to value, it simply does not pull this data.

CodePudding user response:

Try this:

FutureBuilder(
              future: Future.delayed(Duration(seconds: 1))
                  .then((value) => _daoCateg.findAll_categoria()),
              builder: (context, AsyncSnapshot snapshot) {
                if (snapshot.hasData && snapshot.data != null) {
                  final List<registro_categoria> _cadastro = snapshot.data;
                  if(_selectedValue == "Selecione uma categoria"){
                    _selectedValue = _cadastro.first.nome_categoria.toString();
                  }
                  return DropdownButton(
                    onChanged: (value) {
                      setState(() {
                        _selectedValue = value;
                      });
                    },
                    value: _selectedValue,
                    items: _cadastro.map((map) {
                      return DropdownMenuItem(
                        child: Text(map.nome_categoria.toString()),
                        value: map.nome_categoria.toString(),
                      );
                    }).toList(),
                    hint: Text('Selecione uma categoria'),
                  );
                } else {
                  return Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        CircularProgressIndicator(),
                        Text('Carregando favoritos'),
                      ],
                    ),
                  );
                }
              }),
  • Related