'Unhandled Exception: HiveError: Cannot write, unknown type: _File. Did you forget to register an adapter?
I use model of places where I have File object to store the path of images taken by device camera.
I want to store File(image.path) in Hive. But I get :
Unhandled Exception: HiveError: Cannot write, unknown type: _File. Did you forget to register an adapter?
part 'place.g.dart';
@HiveType(typeId: 0)
class Place extends HiveObject {
@HiveField(0)
final String id;
@HiveField(1)
final String name;
@HiveField(2)
final File image;
@HiveField(3)
final Location? location;
Place(this.id, this.name, this.image, this.location);
}
part 'file.g.dart';
@HiveType(typeId: 1)
class ImageFile extends HiveObject {
@HiveField(3)
File file;
ImageFile(this.file);
}
I have initialized and registered the adapters as well.
final dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
Hive.registerAdapter(PlaceAdapter());
Hive.registerAdapter(LocationAdapter());
Hive.registerAdapter(ImageFileAdapter());```
Hive docs recommends to write your own TypeAdapter for other types other than primitive ones?
But I don't have an idea how to write TypeAdapter for File
thx
Solution 1:[1]
I resolved the issue by converting File to Uint8List which is a primitive type supported by Hive.
So when I store a File(image.path) I add it by pickedImage.readAsBytesSync();
and when I read an image from hive box I use Image.memory();
I don't know it is solution or not, but it worked for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sezgin Özcan |
