[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: HiveError: Cannot write, unknown type: _File. Did you forget to register an adapter?
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
when I tried to add a video file I'm getting this error
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: HiveError: Cannot write, unknown type: _File. Did you forget to register an adapter?
**Code sample**
model.dart
@HiveType(typeId: 1)
class Video {
@HiveField(0)
int? id;
@HiveField(1)
final File videoFile;
Video({required this.videoFile, this.id});
}
model.g.dart
class VideoAdapter extends TypeAdapter {
@override
final int typeId = 1;
@override
Video read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Video(
videoFile: fields[0] as File,
);
}
@override
void write(BinaryWriter writer, Video obj) {
writer
..writeByte(1)
..writeByte(0)
..write(obj.videoFile);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is VideoAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
if (!Hive.isAdapterRegistered(VideoAdapter().typeId)) {
Hive.registerAdapter(VideoAdapter());
}
runApp(const MyApp());
}
DB code
Future addVideos(Video value) async {
final videoDB = await Hive.openBox('video_db');
await videoDB.add(value);
videosNotifier.value.add(value);
videosNotifier.notifyListeners();
}
Future onCamaraClick() async {
final capture = await ImagePicker().pickVideo(source: ImageSource.camera);
final file = File(capture!.path);
if (file == null) {
return;
} else {
setState(() {
final videoToAddList = Video(videoFile: file);
addVideos(videoToAddList);
});
}
}
**Version**
- Platform:
-
- Android,
- Flutter version: [e.g. 1.5.4]
-
Flutter: 2.8.0
- Hive version: [e.g. 0.5.0]
-
hive: ^2.0.4
hive_flutter: ^1.1.0
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with model.dart and the generated model.g.dart adapter, then trace registration in main and persistence through addVideos and onCamaraClick. Reproduce the reported error when adding a video and determine the expected supported representation for the File field. Done means the Video record can be persisted without the unknown-type error and the behavior is verified against this example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100