Reputation: 1
final class HiveCacheManager extends CacheManager {
/// [path] is the path to the directory
/// where the Hive database files are stored.
HiveCacheManager({super.path});
@override
Future<void> init({required List<CacheModel> items}) async {
final documentPath = path ?? (await getApplicationDocumentsDirectory()).path;
Hive.defaultDirectory = documentPath;
for (final item in items) {
Hive.registerAdapter('${item.runtimeType}', item.fromDynamicJson);
}
}
@override
void remove() {
Hive.deleteAllBoxesFromDisk();
}
}
final class ProductCache {
ProductCache({required CacheManager cacheManager}) : _cacheManager = cacheManager;
final CacheManager _cacheManager;
Future<void> init() async {
await _cacheManager.init(
items: [
UserCacheModel.empty(),
UserTokenCacheModel.empty(),
],
);
}
late final HiveCacheOperation<UserCacheModel> userCacheOperation = HiveCacheOperation<UserCacheModel>();
late final HiveCacheOperation<UserTokenCacheModel> userTokenCacheOperation =
HiveCacheOperation<UserTokenCacheModel>();
}
Unhandled Exception: Invalid argument(s): Type mismatch. Expected UserCacheModel but got UserTokenCacheModel. E/flutter (11169): #0 _TypeRegistry.fromJson (package:hive/src/impl/type_registry.dart:45:7) E/flutter (11169): #1 _BoxImpl._frameFromJson (package:hive/src/impl/box_impl.dart:80:31) E/flutter (11169): #2 MappedListIterable.elementAt (dart:_internal/iterable.dart:425:31) E/flutter (11169): #3 ListIterator.moveNext (dart:_internal/iterable.dart:354:26) E/flutter (11169): #4 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27) E/flutter (11169): #5 new _GrowableList.of (dart:core-patch/growable_array.dart:150:28) E/flutter (11169): #6 new List.of (dart:core-patch/array_patch.dart:39:18) neden bu hatayı alıyorum
I gonna use two different class model using one common base Hive operation clas.
Upvotes: 0
Views: 78
Reputation: 1399
It's not correct usage.
Hive.registerAdapter('${item.runtimeType}', item.fromDynamicJson);
The correct usage is =>
Hive.registerAdapter<AdapterClass>(AdapterClass());
Upvotes: 0