Filter list with whereIn condition in Flutter?

855

You should not use the keyword in but the method contains to check if your item exists inside categoryList. Moreover you cannot compare values of different types, I'm seeing that you are returning with box.values an Iterable<MainWords>.

I don't know the content of this class but the item variable is of type MainWords and so it cannot be compared with a String object directly.

I am supposing that you have some access to a String value from your class MainWords so you will need to compare this value with your list.

Code Sample

List<String> categoryList = ['6', '13'];
var box = await Hive.openBox<MainWords>('mainWords');

// As I don't know what are MainWords' properties I named it stringValue.
box.values.where((item) => categoryList.contains(item.stringValue)).toList();
Share:
855
Emma Alden
Author by

Emma Alden

Someone who tries to improve her skil in codding.

Updated on December 29, 2022

Comments

  • Emma Alden
    Emma Alden over 1 year

    This code sample works fine.

    var box = await Hive.openBox<MainWords>('mainWords');
    box.values.where((item)  {
          return item.category == "6" || item.category == '13';
        }).toList();
    

    I am trying to filter a list with whereIn condition but it must filter like

    List<String> categoryList = ['6', '13'];
    var box = await Hive.openBox<MainWords>('mainWords');
    box.values.where((item)  {
          return item in categoryList; // just an examle
        }).toList();
    

    How can i achive this?

    • Raine Dale Holgado
      Raine Dale Holgado almost 3 years
      Maybe categoryList.contains(item)?, are you trying to filter the values based on categoryList?. Please clarify what you trying to do