1
_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 a 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.

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • Have you check that each `registro_categoria` has unique `nome_categoria`? – ישו אוהב אותך Mar 16 '22 at 08:41
  • See the related answer: https://stackoverflow.com/questions/60510150/flutter-there-should-be-exactly-one-item-with-dropdownbuttons-value – ישו אוהב אותך Mar 16 '22 at 08:42
  • 1
    What is the initial value of `_selectedValue`? – Josteve Mar 16 '22 at 08:45
  • @Josteve When I start it with an initial value it throws this error as soon as the form is drawn... It doesn't even wait for me to click on the dropdownButton – alyson pereira Mar 16 '22 at 08:59
  • Can you post your full class code? – Josteve Mar 16 '22 at 09:00
  • @Josteve this is my form – alyson pereira Mar 16 '22 at 09:09
  • @Josteve [link](https://github.com/AlysonTrizotto/LocalizaCliente/blob/main/lib/screens/cadastro/formulario_favoritos.dart) – alyson pereira Mar 16 '22 at 09:14
  • @Josteve I sent the link above, the platform has a character limit. So I uploaded it through github. Where you can also see the entire project – alyson pereira Mar 16 '22 at 09:15
  • @ישואוהבאותך Yes, I checked and it really has value. But when I leave a pre-established value for it to start, it pops the error as soon as the form starts. Or when I don't leave it pre-established and I select a field from the DropdownButton it pops the error. That is, when I don't set a value for it to start, it starts without error and returns the items from the third field. – alyson pereira Mar 16 '22 at 09:20
  • @alysonpereira: Try using your first item as the default value. `final List _cadastro = snapshot.data; _selectedValue = _cadastro.first;` – ישו אוהב אותך Mar 16 '22 at 10:47
  • 1
    @ישואוהבאותך, Guys, thank you so much for your help!! You helped me a lot. Solved the problem. Really the '_cadastro.first.name_category.toString()' worked – alyson pereira Mar 16 '22 at 14:39
  • 1
    @Josteve, What was missing was the if that he gave the idea. It worked perfectly!! Thank you so much, I've been beating myself up on this for 3 days rsrs – alyson pereira Mar 16 '22 at 14:40

1 Answers1

1

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'),
                      ],
                    ),
                  );
                }
              }),
Josteve
  • 11,459
  • 1
  • 23
  • 35
  • Guys, thank you so much for your help!! You helped me a lot. Solved the problem. Really the '_cadastro.first.name_category.toString()' worked. What was missing was the if that he gave the idea. It worked perfectly!! Thank you so much, I've been beating myself up on this for 3 days lol – alyson pereira Mar 16 '22 at 14:40