isar / isar/hive

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

Open
#1,016 1 comment 0 reactions 0 assignees View on GitHub
problem
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**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**
```dart
Future 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`:
```dart
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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at defaultKeyComparator and the Box.get entry point described in the issue, then reproduce the null-key case with a String-keyed box. Done means calling get(null) or get(null, defaultValue) no longer raises the reported type-cast error and returns the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.