Hive loses type information when Box is closed, works otherwise?
Question
When I write a Map<String, dynamic> 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<String, dynamic>, it fails with the following error:
type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
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.
import 'package:hive/hive.dart';
void main() async {
await Hive.init('.');
{
final box = await Hive.openBox(_boxName);
// ignore: omit_local_variable_types
final Map<String, dynamic> map = {
'bool': true,
'int': 17,
'string': 'string',
};
await box.put(_key, map);
// await box.close(); // <===================
}
{
final box = await Hive.openBox(_boxName);
try {
final Map<String, dynamic> 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]
I had same problem,
Does anyone have any ideas to solve ?
Thanks.
any update?