hive icon indicating copy to clipboard operation
hive copied to clipboard

defaultKeyComparator - type 'Null' is not a subtype of type 'String' in type cast

Open AlexDochioiu opened this issue 3 years ago • 1 comments

Steps to Reproduce

  1. Create a box
  2. Put anything in it with a String key.
  3. Call box.get(null) or box.get(null, defaultValue)

Code sample

Future<String?> currentSelection = getCurrentSelection()
final selectedItem = walletBox.get(await currentSelection, defaultValue: defaultItem)

Version

  • Platform: Android
  • Flutter version: 3.0.3
  • Hive version: 2.2.2

---- The issue seems to be in defaultKeyComparator:

else if (k2 is String) {
    return (k1 as String).compareTo(k2);
  }

Trying to cast k1 to String when k1 is null

--- Solution a) Add type safety by changing key from dynamic to some generic type that can be specified when opening a box. b) Return instantly null when calling get with a null key

AlexDochioiu avatar Jun 30 '22 15:06 AlexDochioiu

Steps to Reproduce

  1. Create a box
  2. Put anything in it with a String key.
  3. Call box.get(null) or box.get(null, defaultValue)

Code sample

Future<String?> currentSelection = getCurrentSelection()
final selectedItem = walletBox.get(await currentSelection, defaultValue: defaultItem)

Version

  • Platform: Android
  • Flutter version: 3.0.3
  • Hive version: 2.2.2

---- The issue seems to be in defaultKeyComparator:

else if (k2 is String) {
    return (k1 as String).compareTo(k2);
  }

Trying to cast k1 to String when k1 is null

--- Solution a) Add type safety by changing key from dynamic to some generic type that can be specified when opening a box. b) Return instantly null when calling get with a null key

You can provide a keyComparator inside the openBox() method

DenisDoc avatar Jul 21 '23 14:07 DenisDoc