Skip to content

HiveError: Could not read value from box - when using http_cache_hive_store + dio_cache_interceptor (Android & iOS) #187

@aradhwan

Description

@aradhwan

Summary

When using dio_cache_interceptor with http_cache_hive_store, our app intermittently crashes on both Android and iOS with:

HiveError: Could not read value from box. Maybe your box is corrupted.

Disabling HTTP caching (i.e., removing http_cache_hive_store) makes the issue disappear. Because the error occurs while dio_cache_interceptor calls into http_cache_hive_store (which depends on hive_ce), I’m filing this here first.


Affected packages / versions

  • dio_cache_interceptor: 4.0.3
  • http_cache_hive_store: 5.0.1
  • hive_ce: 2.13.2
  • dio: 5.9.0
  • Flutter: 3.35.5
  • Dart: 3.9.2

Platforms

  • ✅ Android (production): crashes observed
  • ✅ iOS (production): crashes observed

Minimal setup (representative)

final dio = Dio();

final cacheStore = HiveCacheStore(
  (await getApplicationDocumentsDirectory()).path,
  // encryptionKey: null (not using encryption)
  // hiveBoxName: 'dio_cache' (default)
);

final cacheOptions = CacheOptions(
      store: cacheStore,
      policy: CachePolicy.request,
      hitCacheOnErrorCodes: [401, 403],
      hitCacheOnNetworkFailure: false,
      maxStale: const Duration(minutes: 10),
      priority: CachePriority.normal,
      cipher: null,
      keyBuilder: CacheOptions.defaultCacheKeyBuilder,
      allowPostMethod: false,
    );

dio.interceptors.add(DioCacheInterceptor(options: cacheOptions));

// Typical request:
final res = await dio.get('https://example.com/api/...');
// Crashes appear when reads hit cache path.

Crash logs (highlights)

Android (Firebase Crashlytics)

Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: HiveError: Could not read value from box. Maybe your box is corrupted.
  at StorageBackendVm.readValue.<fn>(storage_backend_vm.dart:165)
  at _AsyncCompleter.complete (dart:async)
  at ReadWriteSync.syncRead (read_write_sync.dart:16)
  at LazyBoxImpl.get (lazy_box_impl.dart:28)
  at _DioCacheInterceptorUtils._loadCacheResponse (dio_cache_interceptor_cache_utils.dart:51)
  at DioCacheInterceptor.onRequest (dio_cache_interceptor.dart:47)

iOS (Firebase Crashlytics)

Fatal Exception: FlutterError
0  StorageBackendVm.readValue.<fn> + 165 (storage_backend_vm.dart:165)
1  _AsyncCompleter.complete (dart:async)
2  ReadWriteSync.syncRead + 16 (read_write_sync.dart:16)
3  LazyBoxImpl.get + 28 (lazy_box_impl.dart:28)
4  _DioCacheInterceptorUtils._loadCacheResponse + 51 (dio_cache_interceptor_cache_utils.dart:51)
5  DioCacheInterceptor.onRequest + 47 (dio_cache_interceptor.dart:47)

Full crash reports are attached:
Android_issue_stacktrace.txt
iOS_issue_stacktrace.txt


Steps to reproduce (as observed)

  1. Initialize Dio with DioCacheInterceptor and HiveCacheStore (see code above).
  2. Make GET requests that should be cached.
  3. After normal usage (including app background/foreground cycles), a subset of users hit a crash on the read path inside the cache store.
  4. Removing/Disabling caching eliminates the crash.

Expected behavior

Reading from the cache store should not throw HiveError. If a box is corrupted, the store should either:

  • surface a recoverable signal (so the app can evict/ignore the bad entry), or
  • handle corruption internally by evicting the problematic key/box and continuing gracefully.

Actual behavior

HiveError: Could not read value from box. Maybe your box is corrupted. bubbles up while DioCacheInterceptor tries to _loadCacheResponse, causing a production crash on both Android and iOS.


Frequency / impact

  • Observed across multiple sessions/devices in production.
  • Intermittent, but recurring enough to impact users.

Things we’ve tried

  • Disabling caching → no crashes.
  • Ensured single initialization of the cache store.
  • Not using encryption for cache boxes.
  • Changing box name / clearing cache on cold start.

Checklist

  • Bug occurs on Android
  • Bug occurs on iOS
  • Only occurs when http_cache_hive_store is enabled
  • Call chain: DioCacheInterceptorhttp_cache_hive_storehive_ce
  • Crash logs included

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions