hive
hive copied to clipboard
defaultKeyComparator - type 'Null' is not a subtype of type 'String' in type cast
Steps to Reproduce
- Create a box
- Put anything in it with a
Stringkey. - Call
box.get(null)orbox.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
Steps to Reproduce
- Create a box
- Put anything in it with a
Stringkey.- Call
box.get(null)orbox.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
k1to String when k1 is null--- Solution a) Add type safety by changing
keyfromdynamicto some generic type that can be specified when opening a box. b) Return instantlynullwhen callinggetwith a null key
You can provide a keyComparator inside the openBox() method