My Hive Modal Class. Below that you can find my generated class.

@HiveType(typeId: 1)
class Employee {
  Employee({
    required this.employeeId,
    required this.officeId,
    required this.email,
    required this.name,
    required this.position,
    required this.number,
  });

  @HiveField(0)
  final String employeeId;

  @HiveField(1)
  final String officeId;

  @HiveField(2)
  final String email;

  @HiveField(3)
  final String name;

  @HiveField(4)
  final String position;

  @HiveField(5)
  final int number;

  @HiveField(6)
  final String uid = getRandomString(15);

  factory Employee.fromJson(Map<String, dynamic> json) => Employee(
        employeeId: json["employeeId"],
        officeId: json["officeId"],
        email: json["email"],
        name: json["name"],
        position: json["position"],
        number: json["number"],
      );

  Map<String, dynamic> toJson() => {
        "employeeId": employeeId,
        "officeId": officeId,
        "email": email,
        "name": name,
        "position": position,
        "number": number,
      };
}

It is not storing the data which I am passing in uid of random 15 digits. the adapters are generated correctly.

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'employee.dart';

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

class EmployeeAdapter extends TypeAdapter<Employee> {
  @override
  final int typeId = 1;

  @override
  Employee read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return Employee(
      employeeId: fields[0] as String,
      officeId: fields[1] as String,
      email: fields[2] as String,
      name: fields[3] as String,
      position: fields[4] as String,
      number: fields[5] as int,
    );
  }

  @override
  void write(BinaryWriter writer, Employee obj) {
    writer
      ..writeByte(7)
      ..writeByte(0)
      ..write(obj.employeeId)
      ..writeByte(1)
      ..write(obj.officeId)
      ..writeByte(2)
      ..write(obj.email)
      ..writeByte(3)
      ..write(obj.name)
      ..writeByte(4)
      ..write(obj.position)
      ..writeByte(5)
      ..write(obj.number)
      ..writeByte(6)
      ..write(obj.uid);
  }

  @override
  int get hashCode => typeId.hashCode;

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

I have Checked the adapters i am not passing the data through constructor because it only takes constant values. What can i do now?

Help!! Help!!


Solution 1: dayvedev

Do this instead. Pass the dynamic data to the constructor body but not as a parameter. See below.

@HiveType(typeId: 1)
class Employee {
  Employee({
    required this.employeeId,
    required this.officeId,
    required this.email,
    required this.name,
    required this.position,
    required this.number,

  }){
    uid = getRandomString(15);
  }

  @HiveField(0)
  final String employeeId;

  @HiveField(1)
  final String officeId;

  @HiveField(2)
  final String email;

  @HiveField(3)
  final String name;

  @HiveField(4)
  final String position;

  @HiveField(5)
  final int number;

  @HiveField(6)
  late String uid;

  factory Employee.fromJson(Map<String, dynamic> json) => Employee(
    employeeId: json["employeeId"],
    officeId: json["officeId"],
    email: json["email"],
    name: json["name"],
    position: json["position"],
    number: json["number"],
  );

  Map<String, dynamic> toJson() => {
    "employeeId": employeeId,
    "officeId": officeId,
    "email": email,
    "name": name,
    "position": position,
    "number": number,
  };

}