HiveError: Box has already been closed.
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
**Steps to Reproduce**
I am using hivebox to store connections. Each key is separate connection. Connections are added/updated frequently
I am using below code to make sure box is open appropriately but i am still getting
**HiveError: Box has already been closed.** error when i try to access a key in a box
**Code sample**
```
static Future> openIt() async {
var connectionBox = await Hive.openBox(hiveBox);
if (Hive.isBoxOpen(hiveBox) == false) {
await Hive.openBox(hiveBox);
} else {
await connectionBox.close();
connectionBox = await Hive.openBox(hiveBox);
}
return connectionBox;
}
static Future> getUnReadCount(String uid) async {
var connectionBox = await openIt();
var prevData = connectionBox.get(uid);
Map returnMap = {};
try {
returnMap['unreadCount'] = '0';
return returnMap;
} catch (e) {
//this exception is throwing the error
}
}
```
**Version**
- Platform: iOS
- Flutter version: 2.2.1
- Hive version: hive: ^2.0.4, hive_flutter: ^1.0.0
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the supplied openIt and getUnReadCount methods, focusing on the sequence of openBox, close, reopen, and get. Reproduce the error with the stated Flutter and Hive versions, then trace the box lifecycle during frequent updates; done means key access no longer raises HiveError: Box has already been closed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- database, mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100