![](/img/trans.png)
[英]Flutter how to update text on dropdownbutton when using Sqflite to populate the list
[英]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:
我在選擇一個值時得到了這個回報,我已經嘗試了幾種方法,但我仍然不能。 每當我 select 值它返回此錯誤。 有人能幫我嗎?
這是我用來提取類別的塊,它只適用於第三個類別,在此之前它會返回我提到的這個錯誤。
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 時,它被分配給“值:”但是當我 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'),
],
),
);
}
}),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.