0

我对颤动和处理应用程序有点陌生,我需要在本地保存数据以便以后在用户离线时使用它。

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

模态类:

导入“包: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> toJson() {
    final Map<String, dynamic> data = new 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> toJson() {
    final Map<String, dynamic> data = new 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> toJson() {
    final Map<String, dynamic> data = new 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> toJson() {
    final Map<String, dynamic> data = new 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 创建和添加多个适配器,因为我的模态类有多个类?

4

1 回答 1

0

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

您只需要在保存数据之前添加它们,如下所示:

 Hive
      ..init(appDocDir.path)
      ..registerAdapter(DownloadResponseAdapter())
      ..registerAdapter(UserInfoAdapter())
      ..registerAdapter(AppSettingAdapter())
      ..registerAdapter(SealsAdapter())
      ..registerAdapter(DocumentsAdapter()
      );
于 2021-01-04T20:15:45.463 回答