Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter Hive data in flutter by data values?

In Flutter, I using ValueListenableBuilder widget to get list of hive data, and I'm trying to filter my data by data values.

Example:-

Key: 1

name(value) : mydata1

des(value) : mydescription1

value(value) : 1

here in this example I want to filter data by data value called value(value) by help of dropdown,

like:

if (value.compareTo(1) == 1){
 print('All First Value Data Showing Result');
}

Something like that:

Expanded(
  child: ValueListenableBuilder(
    valueListenable: msgbox.listenable(),
    builder: (context, box, _) {
      Map<dynamic, dynamic> raw = box.toMap();
      List list = raw.values.toList();
      return ListView.builder(
        itemCount: list.length,
        itemBuilder: (context, index){
          MsgModel msges = list[index];
          return GestureDetector(
            onDoubleTap: () {},
            child: Padding(
              padding: EdgeInsets.only(left: 8, right: 8),
              child: Column(
                children: [
...

ValueListenableBuilder mycode Image


1 Answers

You can simply filter the list using the where() function. Example:

list.where((item) => item.value == 1)
    .forEach((item) => print('All First Value Data Showing Result'));

This will filter the list and retain objects only where the value is equal to 1.

Or for other people that are using Box to retrieve your values you can do like this example:

Box<Item> itemBox = Hive.box<Item>("Item");
itemBox.values.where((item) => item.value == 1)
       .forEach((item) => print('All First Value Data Showing Result'));

Hope this is what you were searching for.

like image 158
Joshua Moochooram Avatar answered Jun 08 '26 13:06

Joshua Moochooram