android - 如何修复错误 "' WhereIterable<Products >' is not a subtype of type ' List<Products >'"

标签 android flutter

我正在尝试根据类别过滤食品,但不断收到以下错误。
这个想法是使用 Provider 包为我选择的类别手动传递一个字符串,然后过滤并打印与包含食品的列表中的类别匹配的项目。

 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>'
这是虚拟数据的 fragment ,其中我分别在名为类别和产品的列表中定义了类别和食品。我非常有信心最后的过滤器没有很好地定义。
    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);
  }
}
此外,我从中传递字符串以过滤列表的类的 fragment
    class TestClass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final provider1 = Provider.of<DummyData>(context).filter('American');
    print(provider1);
    // TODO: implement build
    return Scaffold(
    );
  }
}

最佳答案

我刚刚弄清楚我哪里出错了:
DummyData类中filter方法中的结果需要转换为List:

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

关于android - 如何修复错误 "' WhereIterable<Products >' is not a subtype of type ' List<Products >'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62972666/

相关文章:

java - Android 权限检查类

android - AlertDialog 在彼此之上显示多个对话框?

android - 解析大型 JSON 响应时出现 OutOfMemoryException

firebase - 按时间顺序显示

flutter - 在Flutter中点击单选按钮时隐藏键盘

flutter - 在文本字段中右键单击鼠标时防止复制粘贴功能 flutter web

javascript - react native : Native modules return nothing

java - 为什么通过 MediaController (Android) 播放声音时 SeekBar 不移动?

flutter - Android 和 iOS 如何用 Flutter 实现振动?

flutter - 如何在flutter中混合nullsafety和not null安全包