Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the error "'WhereIterable<Products>' is not a subtype of type 'List<Products>'"

I'm trying to filter food items based on their category but keep getting the below error. The idea is to manually pass a String for the category of my choice using the Provider package and then filter and print the items that match the category from the list that has the food items.

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

Here is the snippet of the dummy data where I have the categories and food items defined in the lists named categories and products respectively. I'm pretty confident that the filter at the end is not very well defined.

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

Also, the snippet for the class from which I'm passing the String to filter the list

    class TestClass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final provider1 = Provider.of<DummyData>(context).filter('American');
    print(provider1);
    // TODO: implement build
    return Scaffold(
    );
  }
}
like image 265
coolhack7 Avatar asked Jul 18 '20 19:07

coolhack7


1 Answers

I just figured out where I was going wrong:

The result in the filter method in DummyData class needs to be converted to a List:

  List<Products> filter(String title) {
    return products.where((element) => element.category == title).toList();
  }
like image 72
coolhack7 Avatar answered Oct 16 '22 08:10

coolhack7