Unhandled Exception: HiveError: Cannot write, unknown type: CityWeather. Did you forget to register an adapter?
Question I'm getting Unhandled Exception: HiveError: Cannot write, unknown type: CityWeather. Did you forget to register an adapter? when trying to save to DB even though I've created the adapter.
I've seen that people posted similar issues here but the difference is that they used a specific property in the class, and I'm using a String and an Int.
Steps to repro: Saving my CityWeather object to hive.
Code sample cityWeather.dart
import 'package:hive/hive.dart';
part 'cityWeather.g.dart';
@HiveType(typeId: 0)
class CityWeather extends HiveObject {
@HiveField(0)
String cityName;
@HiveField(1)
int isFav;
CityWeather({this.cityName, this.isFav});
}
cityWeather.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'cityWeather.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class CityWeatherAdapter extends TypeAdapter<CityWeather> {
@override
final typeId = 0;
@override
CityWeather read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = <int, dynamic>{
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return CityWeather(
cityName: fields[0] as String,
isFav: fields[1] as int,
);
}
@override
void write(BinaryWriter writer, CityWeather obj) {
writer
..writeByte(2)
..writeByte(0)
..write(obj.cityName)
..writeByte(1)
..write(obj.isFav);
}
}
I'm calling hive like this in my updateUi method:
CityWeather cityWeather = CityWeather(isFav: 1, cityName: 'SomeName');
Hive.box('cityNames').put('current', cityWeather);
which throws the exception.
I've tried saving dart primitives and it worked as it should.
Version
- Platform: Windows
- Flutter version: 1.12.13
- Hive version: 1.4.1+1
I've figured the issue out. The TypeAdapter needs to be initialized while initializing Hive.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocumentDir = await path_provider.getApplicationDocumentsDirectory();
Hive
..init(appDocumentDir.path)
..registerAdapter(CityWeatherAdapter());
runApp(MyApp());
Closing the issue.
what if i have two hive models ????? Its getting error
what if i have two hive models ????? Its getting error
Try to change typeId in other adapter.
Actually I am having the same error but with RxString type, somehow its unable to write
Error message: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: HiveError: Cannot write, unknown type: RxString. Did you forget to register an adapter?
#0 BinaryWriterImpl.write (package:hive/src/binary/binary_writer_impl.dart:338:9)
#1 HabitModelAdapter.write (package:habbit_tracker/model/habit_model.g.dart:33:9)
#2 BinaryWriterImpl.write (package:hive/src/binary/binary_writer_impl.dart:344:24)
I've registered the adapter as well, my main.dart code:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
Hive.registerAdapter(HabitModelAdapter());
box = await Hive.openBox<List<HabitModel>>(BoxConstants.boxName);
runApp(const MyApp());
}
I am trying to add list of model class data, in which the fields are of type RxString, RxDouble so that I can listen to values with GetX