我试着根据食物的类别来过滤食物,但总是出现下面的错误。其思想是使用提供程序包为我选择的类别手动传递一个字符串,然后过滤并打印与包含食品的列表中的类别相匹配的项目。
I/flutter ( 7404): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 7404): The following _TypeError was thrown building TestClass(dirty, dependencies:
I/flutter ( 7404): [_InheritedProviderScope<DummyData>]):
I/flutter ( 7404): type 'WhereIterable<Products>' is not a subtype of type 'List<Products>'下面是虚拟数据的片段,其中我在名为categories和products的列表中分别定义了类别和食品项目。我非常确信,最后的过滤器定义得不是很好。
class DummyData with ChangeNotifier {
List<Categories> categories = [
Categories(
catId: '1',
title: 'American',
imageUrl: 'https://www.recipetineats.com/wp-content/uploads/2016/02/Beef-Hamburgers_7-2.jpg?w=500&h=500&crop=1'
),
Categories(
catId: '2',
title: 'North Indian',
imageUrl: 'https://static.toiimg.com/thumb/53205522.cms?width=1200&height=1200'
),
Categories(
catId: '3',
title: 'Chinese',
imageUrl: 'https://uploads-ssl.webflow.com/5c481361c604e53624138c2f/5c6cd55ca1bcb14248ded5c4_chilli-pork-website-thumbnail-.png'
),
];
List<Products> products = [
Products(
id: '1',
name: 'Mac & Cheese',
preparationTime: '30 mins',
imageUrl: 'https://www.inspiredtaste.net/wp-content/uploads/2018/10/Easy-Creamy-Stovetop-Mac-and-Cheese-1200.jpg',
category: 'American'
),
Products(
id: '2',
name: 'Hamburger',
preparationTime: '30 mins',
imageUrl: 'https://www.recipetineats.com/wp-content/uploads/2016/02/Beef-Hamburgers_7-2.jpg?w=500&h=500&crop=1',
category: 'American'
),
Products(
id: '3',
name: 'Chilli Pork',
preparationTime: '1 Hr',
imageUrl: 'https://uploads-ssl.webflow.com/5c481361c604e53624138c2f/5c6cd55ca1bcb14248ded5c4_chilli-pork-website-thumbnail-.png',
category: 'Chinese'
),
Products(
id: '4',
name: 'Fried Rice',
preparationTime: '15 mins',
imageUrl: 'https://www.saveur.com/resizer/lijLVB5tWYhp-81mavFmDDxy_Xo=/600x600/arc-anglerfish-arc2-prod-bonnier.s3.amazonaws.com/public/SITSUSMWR7A2IQ64GMSPSIOOQE.jpg',
category: 'Chinese'
),
Products(
id: '4',
name: 'Butter Chicken',
preparationTime: '1 Hr',
imageUrl: 'https://static.toiimg.com/thumb/53205522.cms?width=1200&height=1200',
category: 'North Indian'
),
Products(
id: '5',
name: 'Chicken Tikka Masala',
preparationTime: '1 Hr',
imageUrl: 'https://i2.wp.com/spicecravings.com/wp-content/uploads/2017/08/Palak-Paneer-5-500x500.jpg',
category: 'North Indian'
),
List<Categories> get categoryType {
return [...categories];
}
List<Products> get food {
return[...products];
}
List<Products> filter(String title) {
return products.where((element) => element.category == title);
}
}另外,我从其中传递字符串以过滤列表的类的代码片段
class TestClass extends StatelessWidget {
@override
Widget build(BuildContext context) {
final provider1 = Provider.of<DummyData>(context).filter('American');
print(provider1);
// TODO: implement build
return Scaffold(
);
}
}发布于 2020-07-27 05:56:37
我只是找出了我错在哪里:
DummyData类中filter方法的结果需要转换为List:
List<Products> filter(String title) {
return products.where((element) => element.category == title).toList();
}https://stackoverflow.com/questions/62972666
复制相似问题