When I try start the app this error always appears:
HiveError: Cannot read, unknown typeId: 32. Did you forget to register an adapter?
My Hivetype:
import 'package:flutter/cupertino.dart';
import 'package:hive/hive.dart';
part 'auth_token_response_model.g.dart';
@HiveType(typeId: 1)
class AuthTokenResponseModel {
AuthTokenResponseModel({@required this.accessToken, @required this.refreshToken, @required this.accessTokenExpirationDateTime, @required this.idToken, @required this.tokenType});
@HiveField(0)
final String accessToken;
@HiveField(1)
final String refreshToken;
@HiveField(2)
final DateTime accessTokenExpirationDateTime;
@HiveField(3)
final String idToken;
@HiveField(4)
final String tokenType;
}
g.dart file:
```// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'auth_token_response_model.dart';
// ***********************
// TypeAdapterGenerator
// ***********************
class AuthTokenResponseModelAdapter
extends TypeAdapter
@override
AuthTokenResponseModel read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields =
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return AuthTokenResponseModel(
accessToken: fields[0] as String,
refreshToken: fields[1] as String,
accessTokenExpirationDateTime: fields[2] as DateTime,
idToken: fields[3] as String,
tokenType: fields[4] as String,
);
}
@override
void write(BinaryWriter writer, AuthTokenResponseModel obj) {
writer
..writeByte(5)
..writeByte(0)
..write(obj.accessToken)
..writeByte(1)
..write(obj.refreshToken)
..writeByte(2)
..write(obj.accessTokenExpirationDateTime)
..writeByte(3)
..write(obj.idToken)
..writeByte(4)
..write(obj.tokenType);
}
@override
int get typeId => 1;
}
Main:
await Hive.initFlutter();
final appDocumentDir = await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocumentDir.path);
Hive.registerAdapter(AuthTokenResponseModelAdapter());
await HiveService.hiveInit();
hiveInit:
static Future
await Hive.openBox('userlogin');
await Hive.openBox('skipped');
await Hive.openBox('lastSearched');
}
```
Version
@dominikkeller
add
class AuthTokenResponseModelAdapter
extends TypeAdapter<AuthTokenResponseModel> {
//Missing
@override
final typeId = 1;
//Missing
@override
AuthTokenResponseModel read(BinaryReader reader) {
...
Also Try Changing
Hive.registerAdapter(AuthTokenResponseModelAdapter());
to
Hive.registerAdapter< AuthTokenResponseModel >(AuthTokenResponseModelAdapter());
@Mravuri96 thank you so much man, it worked for me.
@Mravuri96 this workaround doesn't worked for me. I have typeId from 0 to 33 and the error says: HiveError: Cannot read, unknown typeId: 69. Did you forget to register an adapter?
Any solution? It suddenly happened, I don't know how.
@Mravuri96 this workaround doesn't worked for me. I have typeId from 0 to 33 and the error says:
HiveError: Cannot read, unknown typeId: 69. Did you forget to register an adapter?Any solution? It suddenly happened, I don't know how.
@joaovirgili try updating your packages to their latest versions, if that doesn't work, can you post a minimal reproducible code.
@Mravuri96 this workaround doesn't worked for me. I have typeId from 0 to 33 and the error says:
HiveError: Cannot read, unknown typeId: 69. Did you forget to register an adapter?
Any solution? It suddenly happened, I don't know how.@joaovirgili try updating your packages to their latest versions, if that doesn't work, can you post a minimal reproducible code.
@Mravuri96
Already done that and the error persists. I guess there's no way to post a reproducible code 'cause it's a huge project , it's not viable but i will try to share some of them.
Flutter doctor:
```Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.5, on Mac OS X 10.15.6 19G73,
locale en)
[✓] Android toolchain - develop for Android devices (Android SDK
version 30.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.47.2)
[✓] Connected device (1 available)
• No issues found!
Versions:
hive: ^1.4.2
hive_generator: ^0.7.0+2
build_runner: ^1.7.4
Error stack
E/flutter (26676): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: HiveError: Cannot read, unknown typeId: 69. Did you forget to register an adapter?
E/flutter (26676): #0 BinaryReaderImpl.read
package:hive/…/binary/binary_reader_impl.dart:322
E/flutter (26676): #1 LojaAdapter.read
package:pepsfield_app_promotores/…/models/rota_diaria_model.g.dart:57
E/flutter (26676): #2 BinaryReaderImpl.read
package:hive/…/binary/binary_reader_impl.dart:325
E/flutter (26676): #3 BinaryReaderImpl.readMap
package:hive/…/binary/binary_reader_impl.dart:212
E/flutter (26676): #4 BinaryReaderImpl.read
package:hive/…/binary/binary_reader_impl.dart:316
E/flutter (26676): #5 RotaDiariaAdapter.read
package:pepsfield_app_promotores/…/models/rota_diaria_model.g.dart:17
E/flutter (26676): #6 BinaryReaderImpl.read
package:hive/…/binary/binary_reader_impl.dart:325
E/flutter (26676): #7 BinaryReaderImpl.readFrame
package:hive/…/binary/binary_reader_impl.dart:273
E/flutter (26676): #8 FrameHelper.framesFromBytes
package:hive/…/binary/frame_helper.dart:17
E/flutter (26676): #9 FrameIoHelper.framesFromFile
package:hive/…/io/frame_io_helper.dart:41
E/flutter (26676):
E/flutter (26676): #10 StorageBackendVm.initialize
package:hive/…/vm/storage_backend_vm.dart:82
E/flutter (26676):
E/flutter (26676): #11 BoxBaseImpl.initialize
package:hive/…/box/box_base_impl.dart:90
E/flutter (26676): #12 HiveImpl._openBox
package:hive/src/hive_impl.dart:90
E/flutter (26676):
E/flutter (26676): #13 HiveImpl.openBox
package:hive/src/hive_impl.dart:111
E/flutter (26676): #14 _LoginControllerBase.initHive
package:pepsfield_app_promotores/…/login/login_controller.dart:169
E/flutter (26676):
E/flutter (26676): #15 _LoginControllerBase.init
package:pepsfield_app_promotores/…/login/login_controller.dart:83
E/flutter (26676): #16 _LoginPageState.initState
package:pepsfield_app_promotores/…/login/login_page.dart:28
E/flutter (26676): #17 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4640
E/flutter (26676): #18 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4476
E/flutter (26676): #19 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3446
E/flutter (26676): #20 Element.updateChild
package:flutter/…/widgets/framework.dart:3214
E/flutter (26676): #21 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4527
E/flutter (26676): #22 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4675
E/flutter (26676): #23 Element.rebuild
package:flutter/…/widgets/framework.dart:4218
E/flutter (26676): #24 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4481
E/flutter (26676): #25 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4666
E/flutter (26676): #26 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4476
E/flutter (26676): #27 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3446
E/flutter (26676): #28 Element.updateChild
package:flutter/…/widgets/framework.dart:3214
E/flutter (26676): #29 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4527
E/flutter (26676): #30 Element.rebuild
package:flutter/…/widgets/framework.dart:4218
E/flutter (26676): #31 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4481
E/flutter (26676): #32 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4476
E/flutter (26676): #33 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3446
E/flutter (26676): #34 Element.updateChild
package:flutter/…/widgets/framework.dart:3214
E/flutter (26676): #35 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5830
E/flutter (26676): #36 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3446
E/flutter (26676): #37 Element.updateChild
package:flutter/…/widgets/framework.dart:3214
E/flutter (26676): #38 SingleChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5830
E/flutter (26676): #39 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3446
E/flutter (26676): #40 Element.updateChild
package:flutter/…/widgets/framework.dart:3214
E/flutter (26676): #41 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4527
E/flutter (26676): #42 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4
Init hive
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter
Hive.registerAdapter<RotasMetadata>(RotasMetadataAdapter());
Hive.registerAdapter<SafeZoneCheck>(SafeZoneCheckAdapter());
Hive.registerAdapter<Pausa>(PausaAdapter());
Hive.registerAdapter<SafeZone>(SafeZoneAdapter());
Hive.registerAdapter<TarefaCheck>(TarefaCheckAdapter());
Hive.registerAdapter<Tarefa>(TarefaAdapter());
Hive.registerAdapter<Check>(CheckAdapter());
Hive.registerAdapter<Loja>(LojaAdapter());
Hive.registerAdapter<ProdutoRuptura>(ProdutoRupturaAdapter());
Hive.registerAdapter<TarefaRuptura>(TarefaRupturaAdapter());
Hive.registerAdapter<ProdutoPreco>(ProdutoPrecoAdapter());
Hive.registerAdapter<TarefaPreco>(TarefaPrecoAdapter());
Hive.registerAdapter<TarefaFotos>(TarefaFotosAdapter());
Hive.registerAdapter<Gondola>(GondolaAdapter());
Hive.registerAdapter<ProdutoEstoque>(ProdutoEstoqueAdapter());
Hive.registerAdapter<TarefaEstoque>(TarefaEstoqueAdapter());
Hive.registerAdapter<Lote>(LoteAdapter());
Hive.registerAdapter<RotaDiaria>(RotaDiariaAdapter());
Hive.registerAdapter<PosicaoComTempo>(PosicaoComTempoAdapter());
Hive.registerAdapter<TarefaGondola>(TarefaGondolaAdapter());
Hive.registerAdapter<ProdutoGondola>(ProdutoGondolaAdapter());
Hive.registerAdapter<Deslocamento>(DeslocamentoAdapter());
Hive.registerAdapter<MarcaConcorrente>(MarcaConcorrenteAdapter());
await Hive.openBox('user');
await Hive.openBox('utils');
await Hive.openBox('preco');
await Hive.openBox('gondola');
The error happens when `user` box is openned, in Loja's adapter generated class:
class LojaAdapter extends TypeAdapter
@override
final int typeId = 18;
@override
Loja read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields =
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Loja(
checkin: fields[0] as Check,
checkout: fields[1] as Check,
cidade: fields[2] as String,
concluida: fields[3] as bool,
estado: fields[4] as String,
estoque: fields[5] as TarefaEstoque,
fotos: fields[6] as TarefaFotos,
lojaId: fields[7] as String,
nomeLoja: fields[8] as String,
nomeRede: fields[9] as String,
precos: fields[10] as TarefaPreco,
redeId: fields[11] as String,
ruptura: fields[12] as TarefaRuptura,
safeZone: (fields[13] as List)?.cast
tarefasCompletas: fields[14] as int,
tarefasTotais: fields[15] as int,
gondolas: fields[16] as TarefaGondola,
);
}
@override
void write(BinaryWriter writer, Loja obj) {
writer
..writeByte(17)
..writeByte(0)
..write(obj.checkin)
..writeByte(1)
..write(obj.checkout)
..writeByte(2)
..write(obj.cidade)
..writeByte(3)
..write(obj.concluida)
..writeByte(4)
..write(obj.estado)
..writeByte(5)
..write(obj.estoque)
..writeByte(6)
..write(obj.fotos)
..writeByte(7)
..write(obj.lojaId)
..writeByte(8)
..write(obj.nomeLoja)
..writeByte(9)
..write(obj.nomeRede)
..writeByte(10)
..write(obj.precos)
..writeByte(11)
..write(obj.redeId)
..writeByte(12)
..write(obj.ruptura)
..writeByte(13)
..write(obj.safeZone)
..writeByte(14)
..write(obj.tarefasCompletas)
..writeByte(15)
..write(obj.tarefasTotais)
..writeByte(16)
..write(obj.gondolas);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is LojaAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
```
I have the same error as in initial post. Initially It was running well today but just after some hot-restart stopped to work. No code change has been done at the hive part at all.
I have the same issue.
Unhandled Exception: HiveError: Cannot read, unknown typeId: 32. Did you forget to register an adapter?
Possible solution what worked for me is to register adapters BEFORE calling await Hive.initFlutter();
Possible reasons:
You have to register adapter to read/write type. The adapters must be registered before any openBox requests. Because openBox uses adapters to read data from the disk.
Don't change model typeId after writing the data to the box, otherwise you'll have to remove / clear database from the disk after changing typeId.
I solved my problem by doing these mymodel.deleteFromDisk()
you should try the same
@totalerex one thing you should always keep in mind is to always register the adapters before opening the box of the entity that the said adapter works with. this will not cause any problem when the box is empty but when the box has some entries. you will get this error. if you hot restart the app and you're getting this error. possibly this is the reason.
Just register all adapters before opening any box
This also happened to me and I discovered what the cause was. I was using a box for keeping user preferences, holding various types of objects inside it. I no longer needed a certain type of object so I deleted the object declaration and removed that object adapter from the code. And then it crashed.
I do not know how to proceed. I cannot delete the box because I have other user preferences inside. The only solution is to keep the old object declaration and the adapter. This isn't ideal because I'm keeping old unused code. Any ideas?
@totalerex one thing you should always keep in mind is to always register the adapters before opening the box of the entity that the said adapter works with. this will not cause any problem when the box is empty but when the box has some entries. you will get this error. if you hot restart the app and you're getting this error. possibly this is the reason.
Just register all adapters before opening any box
You were absolutely right, thank you soo much. The trick is to register all your adapters before opening any type of box...I also did more investigating on my side and found that I was also storing instances of different data types on the same field which I also suspect is what is causing my problem
Adding Hive.ignoreTypeId<YourModel>(0); after registering the TypeAdapter fixed the error Unhandled Exception: HiveError: Cannot read, unknown typeId: 32. Did you forget to register an adapter? for me
Most helpful comment
@dominikkeller
add
Also Try Changing
to