isar / isar/hive

Map<String,dynamic> from Hive doesn't behave like a proper Map<String,dynamic>

Open
#522 17 comments 11 reactions 0 assignees View on GitHub
help wanted problem
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Version**
- Platform: iOS, Android
- Flutter version: 1.22.3
- Hive version: 1.4.4
- Flutter Hive version: 0.3.1

Hello guys, so I'm facing a difficulty understanding what type of Map does Hive returns. Below is my code:

**Code sample**
```dart
Future>> readFromHive() async {
final box = await Hive.openBox('storageName');
final result = box.toMap().map(
(k, e) => MapEntry(
k.toString(),
Map.from(e),
),
);

return result.values.toList();

/// Let's say the result is:
/// ```
/// [ { name: 'John Doe', email: 'johndoe@gmail.com'} ]
/// ```
}

final results = await readFromHive();

print(results[0]); // { name: John Doe, email: johndoe@gmail.com}
print(results[0]['name']); // null
print(results[0]['email']); // null
print(results[0].runtimeType) // _InternalLinkedHashMap
```

Here's the weird things:
1. If it's not a `Map` why doesn't it crash the moment it exits the `readFromHive()`?
2. If it's a `Map` why does `print(results[0]['name']);` prints null, while `print(results[0])` prints correct result?
3. I actually uses `@JsonSerializable` package. And I always need to pass `anyMap: true` in order for the `fromJson` to work properly. I never feels to pass `anyMap: true` if I don't deal with Hive.
4. I use VS Code, and I put a breakpoint on the print statement. When I inspect the type of `results[0]`, the type truly says `Map`

So, the question is like the title, why does it feels like Hive Map is not really acts like a Map? And how to make it a "true" `Map? Or is this intended? Maybe I misunderstand how Hive works?

Thanks

P.S. I can't use Hive's built in json parser since in a clean architecture, the entity and data source should not even care which local storage library I use.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the readFromHive example, particularly box.toMap() and Map.from(e), and reproduce the reported key lookups on the listed Flutter and Hive versions. Compare the runtime map behavior with the printed contents and determine whether the result is intended; document the cause or provide a minimal correction if a defect is confirmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
databases, mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.