Hive While inserting Data checking is data exist or not.
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
**Steps to Reproduce**
Here i am inserting new object into Hive db but before inserting i am check is data exist or not if exists i want to add or else or i wan to use putAt method to update data. but when i reading and checking data using with firstWhere() i am getting this error.
**Code sample**
```
//Insert Data
Future insertData(ApiLogs apiLog) async {
try {
_logsInfoBox = await Hive.openBox(HiveDbTables.API_LOGS_TABLE);
var logsList = await _logsInfoBox.values.toList();
if (logsList.length > 0) {
ApiLogs? apiLogsModel = logsList.firstWhere(
(element) => element.apiCallName == apiLog.apiCallName,orElse: () => null);
if (apiLogsModel != null) {
print("Result variable name is : ${apiLogsModel.toString()}");
print("Result key is : ${apiLogsModel.key.toString()}");
_logsInfoBox.putAt(apiLogsModel.key, apiLogsModel);
} else {
await _logsInfoBox.add(apiLog);
}
} else {
await _logsInfoBox.add(apiLog);
}
print("Logs Box count ${_logsInfoBox.length}");
} on Exception catch (e) {
await _logsInfoBox.add(apiLog);
print("Exception in LogsInsertion ${e.toString()}");
}finally{
print("Proposal Codes List After inserted ${_logsInfoBox.length}");
}
}
Unhandled Exception: type '() => Null' is not a subtype of type '(() => ApiLogs)?' of 'orElse'
```
**Version**
- Platform: Android
- Flutter version:
- Hive version:
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the provided insertData example and inspect the firstWhere call, especially its orElse callback and the ApiLogs collection type. Confirm whether the exception originates in Hive or in the Dart collection usage; done means documenting or correcting the demonstrated behavior with a focused reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- database
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100