如何使用 hive 为单个模态类注册多个适配器

问题描述

我对 Flutter 有点陌生,正在开发需要在本地保存数据以便稍后用户离线时使用的应用程序。

我有一个包含多个内部类的模态类:

模态类:

import 'package:hive/hive.dart';

part 'DownloadResponse.g.dart';

@HiveType(typeId: 1)
class DownloadResponse extends HiveObject {
  @HiveField(0)
  UserInfo userInfo;
  @HiveField(1)
  AppSetting appSetting;
  @HiveField(2)
  List<Seals> seals;
  @HiveField(3)
  String success;
  @HiveField(4)
  String message;

  DownloadResponse(
      {this.userInfo,this.appSetting,this.seals,this.success,this.message});

  DownloadResponse.fromJson(Map<String,dynamic> json) {
    userInfo = json['userInfo'] != null
        ? new UserInfo.fromJson(json['userInfo'])
        : null;
    appSetting = json['appSetting'] != null
        ? new AppSetting.fromJson(json['appSetting'])
        : null;
    if (json['seals'] != null) {
      seals = new List<Seals>();
      json['seals'].forEach((v) {
        seals.add(new Seals.fromJson(v));
      });
    }
    success = json['success'];
    message = json['message'];
  }

  Map<String,dynamic> toJson() {
    final Map<String,dynamic> data = new Map<String,dynamic>();
    if (this.userInfo != null) {
      data['userInfo'] = this.userInfo.toJson();
    }
    if (this.appSetting != null) {
      data['appSetting'] = this.appSetting.toJson();
    }
    if (this.seals != null) {
      data['seals'] = this.seals.map((v) => v.toJson()).toList();
    }
    data['success'] = this.success;
    data['message'] = this.message;
    return data;
  }
}

@HiveType(typeId: 2)
class UserInfo extends HiveObject {
  String fullName;
  String mobileLastSyncDate;

  UserInfo({this.fullName,this.mobileLastSyncDate});

  UserInfo.fromJson(Map<String,dynamic> json) {
    fullName = json['full_name'];
    mobileLastSyncDate = json['mobile_last_sync_date'];
  }

  Map<String,dynamic>();
    data['full_name'] = this.fullName;
    data['mobile_last_sync_date'] = this.mobileLastSyncDate;
    return data;
  }
}

@HiveType(typeId: 3)
class AppSetting extends HiveObject {
  String appWebviewHeight;
  String appScreenHeaderSealScan;
  String appScreenHeaderSealInfo;
  String appScreenHeaderPicture1;
  String appScreenHeaderPicture2;

  AppSetting(
      {this.appWebviewHeight,this.appScreenHeaderSealScan,this.appScreenHeaderSealInfo,this.appScreenHeaderPicture1,this.appScreenHeaderPicture2});

  AppSetting.fromJson(Map<String,dynamic> json) {
    appWebviewHeight = json['app_webview_height'];
    appScreenHeaderSealScan = json['app_screen_header_seal_scan'];
    appScreenHeaderSealInfo = json['app_screen_header_seal_info'];
    appScreenHeaderPicture1 = json['app_screen_header_picture_1'];
    appScreenHeaderPicture2 = json['app_screen_header_picture_2'];
  }

  Map<String,dynamic>();
    data['app_webview_height'] = this.appWebviewHeight;
    data['app_screen_header_seal_scan'] = this.appScreenHeaderSealScan;
    data['app_screen_header_seal_info'] = this.appScreenHeaderSealInfo;
    data['app_screen_header_picture_1'] = this.appScreenHeaderPicture1;
    data['app_screen_header_picture_2'] = this.appScreenHeaderPicture2;
    return data;
  }
}

@HiveType(typeId: 4)
class Seals extends HiveObject {
  String sealId;
  String sealHtml;
  List<Documents> documents;
  Seals({this.sealId,this.sealHtml,this.documents});

  Seals.fromJson(Map<String,dynamic> json) {
    sealId = json['seal_id'];
    sealHtml = json['seal_html'];
    if (json['documents'] != null) {
      documents = new List<Documents>();
      json['documents'].forEach((v) {
        documents.add(new Documents.fromJson(v));
      });
    }
  }

  Map<String,dynamic>();
    data['seal_id'] = this.sealId;
    data['seal_html'] = this.sealHtml;
    if (this.documents != null) {
      data['documents'] = this.documents.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

@HiveType(typeId: 5)
class Documents extends HiveObject {

  String documentId;
  String documentName;
  String documentLink;

  Documents({this.documentId,this.documentName,this.documentLink});

  Documents.fromJson(Map<String,dynamic> json) {
    documentId = json['document_id'];
    documentName = json['document_name'];
    documentLink = json['document_link'];
  }

  Map<String,dynamic>();
    data['document_id'] = this.documentId;
    data['document_name'] = this.documentName;
    data['document_link'] = this.documentLink;
    return data;
  }
}

这是我尝试在配置单元中保存数据的逻辑:

   // We get the current app directory
WidgetsFlutterBinding.ensureInitialized();
final appDocDir = await getApplicationDocumentsDirectory();
// We initialize Hive and we give him the current path
Hive
  ..init(appDocDir.path)
  ..registeradapter(DownloadResponseAdapter());
var Box = await Hive.openBox('driverData');

//Box.put('ew32',DownloadResponse('BMW','test',2002));

UserInfo userInfo = downloadResponse.userInfo;
AppSetting appSetting = downloadResponse.appSetting;
List<Seals> sealList = downloadResponse.seals;
String success = downloadResponse.success;
String message = downloadResponse.message;


await Box.put('driverData',DownloadResponse()
  ..userInfo = userInfo
  ..appSetting = appSetting
  ..seals = sealList
  ..success =  success
  ..message = message);

print(Box.get('driverData'));

我在 Box.put() 运行时得到这个异常:

未处理的异常:HiveError:无法写入,未知类型:UserInfo。你是不是忘记注册适配器了

我的问题是如何使用 hive 创建和添加多个适配器,因为我的模态类有多个类?

解决方法

我得到了同样的答案。您将在自动生成的文件中拥有所有可用的适配器。

您只需要在像这样保存数据之前添加它们:

 Hive
      ..init(appDocDir.path)
      ..registerAdapter(DownloadResponseAdapter())
      ..registerAdapter(UserInfoAdapter())
      ..registerAdapter(AppSettingAdapter())
      ..registerAdapter(SealsAdapter())
      ..registerAdapter(DocumentsAdapter()
      );