hive icon indicating copy to clipboard operation
hive copied to clipboard

Get element by key won't work with keyComparator

Open CripyIce opened this issue 3 years ago • 0 comments

Question Once I open a box using keyComparator functions like Hive.box<ShoppingList>(shoppingListBox).get(id) and Hive.box<ShoppingList>(shoppingListBox).containsKey(id) won't work. After removing keyComparator when opening a box it works fine.

Code sample

const shoppingListBox = 'shoppingListBox';

await Hive.openBox<ShoppingList>(shoppingListBox, keyComparator: (a, b) => -1); //DESC order

final id = const Uuid().v4();
await Hive.box<ShoppingList>(shoppingListBox).put(id, ShoppingList('Item name', [], DateTime.now()));

final shoppingList = Hive.box<ShoppingList>(shoppingListBox).get(id); //returns null
final shoppingListExists = Hive.box<ShoppingList>(shoppingListBox).containsKey(id); //returns false

//shopping_list.dart
@HiveType(typeId: 0)
class ShoppingList extends HiveObject {
  @HiveField(0)
  String name;

  @HiveField(1)
  List<Product> products;

  @HiveField(2)
  DateTime? dateAdded;

  ShoppingList(this.name, this.products, this.dateAdded);
}

Version

  • Platform: Mac
  • Flutter version: 3.0.4
  • Hive version: 2.2.3

CripyIce avatar Jul 07 '22 11:07 CripyIce