isar / isar/hive

error when I add a new data to my box

Open
#1,105 2 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Question**
I have an error when I try to add a new data to my boxpatient
this new data is added to another list called ListNote which is in my list

**ERR Hive**
```
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: HiveError: Cannot write, unknown type: ListNote. Did you forget to register an adapter?
#0 BinaryWriterImpl.write
package:hive/…/binary/binary_writer_impl.dart:338
#1 BinaryWriterImpl.writeList
package:hive/…/binary/binary_writer_impl.dart:223
#2 BinaryWriterImpl._writeList
package:hive/…/binary/binary_writer_impl.dart:390
#3 BinaryWriterImpl.write
package:hive/…/binary/binary_writer_impl.dart:329
#4 PatientsAdapter.write
package:medicalnote/models/listpatient.g.dart:50
#5 BinaryWriterImpl.write
package:hive/…/binary/binary_writer_impl.dart:344
#6 BinaryWriterImpl.writeFrame
package:hive/…/binary/binary_writer_impl.dart:282
#7 StorageBackendVm.writeFrames.
package:hive/…/vm/storage_backend_vm.dart:128
#8 ReadWriteSync.syncWrite.
package:hive/…/vm/read_write_sync.dart:26

```

**Here is my model**
```dart
import 'package:hive_flutter/hive_flutter.dart';

part 'listpatient.g.dart';

@HiveType(typeId: 0)
class Patients {
@HiveField(0)
final String? name;

@HiveField(1)
final String? firstname;

@HiveField(3)
final String? dateofbirth;

@HiveField(4)
final String? email;

@HiveField(5)
final String? numero;

@HiveField(6)
final DateTime? date;

@HiveField(7)
final int? id;

@HiveField(8)
final List? listOfNotes;

const Patients({
this.name,
this.firstname,
this.dateofbirth,
this.email,
this.numero,
this.date,
this.id,
this.listOfNotes,
});
}

@HiveType(typeId: 0)
class ListNote {
@HiveField(1)
final String? title;

@HiveField(2)
final String? note;

@HiveField(3)
final String? conclusion;

ListNote({
this.title,
this.note,
this.conclusion,
});
}
```

**My model generate**

```dart
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'listpatient.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class PatientsAdapter extends TypeAdapter {
@override
final int typeId = 0;

@override
Patients read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Patients(
name: fields[0] as String?,
firstname: fields[1] as String?,
dateofbirth: fields[3] as String?,
email: fields[4] as String?,
numero: fields[5] as String?,
date: fields[6] as DateTime?,
id: fields[7] as int?,
listOfNotes: (fields[8] as List?)?.cast(),
);
}

@override
void write(BinaryWriter writer, Patients obj) {
writer
..writeByte(8)
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.firstname)
..writeByte(3)
..write(obj.dateofbirth)
..writeByte(4)
..write(obj.email)
..writeByte(5)
..write(obj.numero)
..writeByte(6)
..write(obj.date)
..writeByte(7)
..write(obj.id)
..writeByte(8)
..write(obj.listOfNotes);
}

@override
int get hashCode => typeId.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is PatientsAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

class ListNoteAdapter extends TypeAdapter {
@override
final int typeId = 0;

@override
ListNote read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return ListNote(
title: fields[1] as String?,
note: fields[2] as String?,
conclusion: fields[3] as String?,
);
}

@override
void write(BinaryWriter writer, ListNote obj) {
writer
..writeByte(3)
..writeByte(1)
..write(obj.title)
..writeByte(2)
..write(obj.note)
..writeByte(3)
..write(obj.conclusion);
}

@override
int get hashCode => typeId.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ListNoteAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
```

**Here is how I add a new note to my box**
```dart
void initState() {
super.initState();
boxPatient = Hive.box('Patient');
}

void _addNote(String newtitle, String newnote, String newconclusion) {
final newNOTE = Patients(
listOfNotes: [
ListNote(title: newtitle, note: newnote, conclusion: newconclusion)
],
);
boxPatient.add(newNOTE);
Navigator.of(context).pop();
}
```

**Version**
- Platform: iOS, Android
- Flutter version: 3.3.7 lastest version
- hive: ^2.2.3
- hive_flutter: ^1.1.0
- hive_generator: ^2.0.0
- build_runner: ^2.3.2

**Thanks a lot for your help :)**

Contributor guide

No contributing guide indexed for this repository

Research direction

Review the model in listpatient.dart and the generated adapters in listpatient.g.dart, then inspect the Hive setup before writing Patients with a ListNote. Done means adding the note succeeds without the unknown-type error on iOS and Android.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.