How to filter Hive data in flutter by data values?

20k views Asked by At

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

3

There are 3 answers

0
SAI LIN HTUT On

To Edit Your Model Object ,you will need hive model key soon and later. For that I have some kind of filtering keys specific and use those to read data. Do not use index from box.values because it can misleading issue when you delete or update some data on a model like box.putAt(index,updatedModel) or box.deleteAt(index,toBeDeletedModel) when multi selecting models.

dbBox = Box<YourModel>();
final filterdList =
            dbBox.values.where((element) => element.isFavourite!);

final filteredKeys = filterdList.map((element) {
         return dbBox.keyAt(dbBox.values.toList().indexOf(element));
        }).toList();
List<Widget> myLists = filteredKeys.map((key) { 
          final currentModel = dbBox.get(key);
          return YourModelCardWidget(modelKey : key , model : currentModel);
        }).toList();
 // then, you can use that key below ..
 // dbBox.put(key, updatedModel) ( Update ) 
 // dbBox.delete(key, currentModel)  ( Delete )
 
0
rip87 On

this is simple code .

          var filteredUsers = monstersBox.values
          .where((Monster) => Monster.name == "Vampire")
          .toList();
      print(filteredUsers.length);

and this is my class :

@HiveType(typeId: 0)
class Monster {
  @HiveField(0)
  String? name;
  @HiveField(1)
  int? level;
  Monster(this.name, this.level);
}
0
Joshua Moochooram On

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.