在我的flutter应用程序中,我有一个下拉列表,下面是如何将数据加载到其中。
Consumer<ProductsImpl>(
builder: (context, data, child) {
print("consumer running");
return DropdownButton(
hint: Text(
"Please Select ",
style: TextStyle(
fontSize: 14,
),
),
items: data.geProductAndTypeList.map((info) {
return DropdownMenuItem(
child: new Text(info,
style: TextStyle(
fontSize: 14,
)),
value: data,
);
}).toList(),
onChanged: (String newValue) {
setState(() {
_productdropDown = newValue;
print(newValue);
});
},
value: _productdropDown);
},
),
下面是您可以找到getProductAndTypeList
class ProductsImpl with ChangeNotifier {
NavLinks _navLinks = new NavLinks();
List<FreshProducts> _freshProductList = [];
List<String> _productAndTypeList = [];
get geProductAndTypeList => _productAndTypeList;
Future<void> geFreshProductsBySpecies(int id) async {
try {
var data = await http.get(_navLinks.getFreshProductsBySpecies(id));
var jsonData = convert.json.decode(data.body).cast<Map<String, dynamic>>();
_freshProductList = jsonData
.map<FreshProducts>((json) => new FreshProducts.fromJson(json))
.toList();
print("Product sIZE: " + _freshProductList.length.toString());
} catch (error) {
throw error;
}
//notifyListeners();
}
Future<void> buildProductAndTypeList(int speciesID) async{
print("asasas");
for(int i=0; i < _freshProductList.length ; i++)
{
if(_freshProductList[i].productSpecies.idproductSpecies == speciesID)
{
String str = _freshProductList[i].productCategory.name + ", "+ _freshProductList[i].productType.type;
if(!_productAndTypeList.contains(str))
{
_productAndTypeList.add(str);
print(str);
}
}
}
notifyListeners();
}
}
构建下拉列表时,我遇到以下错误。
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown building Consumer<ProductsImpl>(dirty, dependencies: [_DefaultInheritedProviderScope<ProductsImpl>]):[39;49m
type 'List<dynamic>' is not a subtype of type 'List<DropdownMenuItem<String>>'
[38;5;248mEither the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md
[39;49m
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mConsumer<ProductsImpl>[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;248m#0 ProductUIState._buildForm.<anonymous closure>[39;49m
[38;5;248m#1 Consumer.buildWithChild[39;49m
[38;5;248m#2 SingleChildStatelessWidget.build[39;49m
[38;5;244m#3 StatelessElement.build[39;49m
[38;5;248m#4 SingleChildStatelessElement.build[39;49m
[38;5;244m...[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
我该如何解决这个问题?
答案 0 :(得分:0)
答案是使用指定的返回类型为_productAndTypeList
创建自定义get方法。使用get geProductAndTypeList => _productAndTypeList;
时,它会返回动态信息。
我做了以下方法。
List<String> geProductAndTypeList()
{
return _productAndTypeList;
}