首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复错误"'WhereIterable<Products>‘不是’List<Products>‘类型的子类型“

如何修复错误"'WhereIterable<Products>‘不是’List<Products>‘类型的子类型“
EN

Stack Overflow用户
提问于 2020-07-19 03:05:49
回答 1查看 4.3K关注 0票数 9

我试着根据食物的类别来过滤食物,但总是出现下面的错误。其思想是使用提供程序包为我选择的类别手动传递一个字符串,然后过滤并打印与包含食品的列表中的类别相匹配的项目。

代码语言:javascript
复制
 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的列表中分别定义了类别和食品项目。我非常确信,最后的过滤器定义得不是很好。

代码语言:javascript
复制
    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);
  }
}

另外,我从其中传递字符串以过滤列表的类的代码片段

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

回答 1

Stack Overflow用户

发布于 2020-07-27 05:56:37

我只是找出了我错在哪里:

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

代码语言:javascript
复制
  List<Products> filter(String title) {
    return products.where((element) => element.category == title).toList();
  }
票数 31
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62972666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档