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();
Comments
-
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 almost 3 yearsMaybe
categoryList.contains(item)
?, are you trying to filter the values based oncategoryList
?. Please clarify what you trying to do
-