在 Flutter 中使用 sqflite 填充 DropdownButton,选择值(OnChange)时 returns 错误

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

_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: 

当 selecting 值时,我得到了这个 return,我已经尝试了几种方法,但我仍然不能。每当我 select 值时,它 return 就会出现此错误。有人可以帮助我吗?

这是我用来提取类别的块,它只适用于第三个类别,在此之前它 return 是我提到的这个错误。

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'),
                      ],
                    ),
                  );
                }
              }),

这是我用来组织数据库搜索的class:

 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;
}

一执行调试,FutureBuilder 就从数据库中给我带来数据。当我 select 例如 DropdownButton 的项目 3 或 4 它被分配给 'value:' 但是当我 select 1 和 2 它没有分配给值时,它根本不拉这个数据.

试试这个:

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'),
                      ],
                    ),
                  );
                }
              }),