_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:
選擇值時我得到了這個回報,我已經嘗試了幾種方法,但我仍然不能。每當我選擇該值時,它都會回傳此錯誤。有人能幫我嗎?
這是我用來提取類別的塊,它只能從第三個類別開始作業,在此之前它會回傳我這樣提到的這個錯誤。
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 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 會立即將資料庫中的資料帶給我。例如,當我選擇 DropdownButton 的第 3 項或第 4 項時,它被分配給“值:”,但是當我選擇 1 和 2 時,它沒有分配給值,它根本不會提取此資料。
uj5u.com熱心網友回復:
嘗試這個:
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'),
],
),
);
}
}),
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/445217.html
