isar / isar/hive

Hive loses type information when Box is closed, works otherwise?

Open
#562 2 comments 4 reactions 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Question**
When I write a `Map` to a Box and then read it back, the behavior is different depending on whether the box is closed or not between the write and the read. When I close the box and try to assign the read value to `Map`, it fails with the following error:
```
type '_InternalLinkedHashMap' is not a subtype of type 'Map'
```

This makes issues for a Flutter application, where I store some state in one run, and then start the app and try to retrieve the persisted state.

Is this expected?

**Code sample**
The following snippet (pure Dart) works, but when the line marked with `// <===================` is uncommented, the program will fail.

```dart
import 'package:hive/hive.dart';

void main() async {
await Hive.init('.');

{
final box = await Hive.openBox(_boxName);
// ignore: omit_local_variable_types
final Map map = {
'bool': true,
'int': 17,
'string': 'string',
};
await box.put(_key, map);
// await box.close(); // <===================
}

{
final box = await Hive.openBox(_boxName);
try {
final Map map = box.get(_key);
print(map);
} on Object catch (e, trace) {
print(e);
print(trace);
} finally {
await box.close();
}
}

await Hive.deleteBoxFromDisk(_boxName);
}

const _boxName = 'testBox';
const _key = 'key';
```

**Version**
- Platform: iOS, Mac (tested both with Flutter 1.22.6 on an iOS simulator, and pure Dart CLI app on a Mac)
- Flutter version: [1.22.6]
- Hive version: [1.4.4+1]

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.