Code Bug Fix: Hive registerAdapter not accepting id

Original Source Link

I have made a class

import 'package:hive/hive.dart';
part 'CLASStrips.g.dart';


@HiveType()
class Trips{
  @HiveField(0)
  String tripName;

  @HiveField(1)
  var date;
}

and created its adapter

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'CLASStrips.dart';

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

class TripsAdapter extends TypeAdapter<Trips> {
  @override
  Trips read(BinaryReader reader) {
    var numOfFields = reader.readByte();
    var fields = <int, dynamic>{
      for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return Trips()
      ..tripName = fields[0] as String
      ..date = fields[1] as dynamic;
  }

  @override
  void write(BinaryWriter writer, Trips obj) {
    writer
      ..writeByte(2)
      ..writeByte(0)
      ..write(obj.tripName)
      ..writeByte(1)
      ..write(obj.date);
  }
}


and in the main function I am trying to open box and register adapter

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  final appDocumentDir = await path_provider.getApplicationDocumentsDirectory();
  Hive.init(appDocumentDir.path);
  final tripsBox = await Hive.openBox('tripsBox');
  Hive.registerAdapter(TripsAdapter(),33); 

  runApp(MyHomePage());


}

but the registerAdapter function is giving message

Too many positional arguments: 1 expected, but 2 found.
Try removing the extra arguments.

How should I fix it?

import 'package:hive/hive.dart';
part 'CLASStrips.g.dart';


@HiveType()
class Trips{
  @HiveField(0)
  String tripName;

  @HiveField(1)
  var date;

Hive type id should be added to @HiveType().

like so.

@HiveType(TypeId: 33)

You no longer need to add it while registering adapter.

Tagged : / /

Leave a Reply

Your email address will not be published. Required fields are marked *