如何解决错误“'WhereIterable <Products>不是'List <Products>'类型的子类型”

时间:2020-07-18 19:05:49

标签: android flutter

我正在尝试根据食品类别对食品进行过滤,但始终出现以下错误。 这个想法是使用Provider程序包为我选择的类别手动传递一个String,然后从包含食物项目的列表中筛选并打印与该类别匹配的项目。

 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>'

这是虚拟数据的摘录,其中我分别在名为类别和产品的列表中定义了类别和食品。我非常有信心最后的过滤器定义不是很好。

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

此外,我要从中传递String来过滤列表的类的代码段

    class TestClass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final provider1 = Provider.of<DummyData>(context).filter('American');
    print(provider1);
    // TODO: implement build
    return Scaffold(
    );
  }
}

1 个答案:

答案 0 :(得分:5)

我刚刚弄清楚哪里出了问题:

DummyData类的filter方法中的结果需要转换为List:

  List<Products> filter(String title) {
    return products.where((element) => element.category == title).toList();
  }