Dart Hive 不存储预定义变量

Dart Hive Not Storing pre defined variable

我的 Hive 模态 Class。 在下面你可以找到我生成的 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,
      };
}

它没有存储我在随机 15 位数字的 uid 中传递的数据。 适配器已正确生成。

// 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;
}

我已经检查了我没有通过构造函数传递数据的适配器,因为它只采用常量值。我现在可以做什么?

求助!! 求助!!

改为这样做。将动态数据传递给构造函数主体,但不作为参数传递。见下文。

@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,
  };

}