deleteBoxFromDisk causes uncatchable error
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
**Use case**
I wanted to implement a mechanism that will clear the database if there is a problem with opening the box. This was to protect the app from the error of type cast when app is updated.
For example, there is a class named `CustomModel`:
```
@HiveType(typeId: 0)
class CustomModel {
@HiveField(0)
final int value;
const CustomModel({
required this.value,
});
}
```
Let's say we want to change type of `value` to `String` but we already have data stored in database with type `int`. Obviously this will throw an error while opening the box.
```
type 'int' is not a subtype of type 'String' in type cast
```
So I decided to write method using `deleteBoxFromDisk` on opening error.
```
if (!Hive.isBoxOpen(boxName)) {
Hive.registerAdapter(CustomModelAdapter());
try {
box = await Hive.openBox(boxName);
} catch (e) {
await Hive.deleteBoxFromDisk(boxName);
box = await Hive.openBox(boxName);
}
}
```
**Problem**
Even though the application works as expected, an error appears in the terminal. It seems to `deleteBoxFromDisk` causes the same problem, as I mentioned before:
```
type 'int' is not a subtype of type 'String' in type cast
```
I debugged it and it looks like the problem was with `await file.exists()` with `*.hive` file in `backend_manager.dart` but I am not sure if debugger is right about it.
First of all, I don't understand why this error occurs. What's worse this error is uncatchable in try catch:
```
try {
box = await Hive.openBox(boxName);
} catch (e) {
try {
await Hive.deleteBoxFromDisk(boxName);
} catch (e2) {
//never gets here
} finally {
box = await Hive.openBox(boxName);
}
}
```
**Version**
- Platform: iOS, Android
- Flutter version: 2.10.0
- Hive version: 2.2.3
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.