gpt4 book ai didi

flutter - 如何在 hive 对象中编写自定义类对象?

转载 作者:行者123 更新时间:2023-12-03 22:00:27 28 4
gpt4 key购买 nike

在flutter中使用hive时,我使用了我的自定义类对象Profile profile在 hive 对象中。

因此,最初,我将自定义类对象( Profile profile )设置为 null在 Hive 框中添加时。

以下是我的 Hive 类(class):

import 'dart:convert';
import 'package:hive/hive.dart';
import 'package:lpa_exam/src/model/listofexams.dart';
import 'package:lpa_exam/src/model/profile.dart';
part 'hiveprofile.g.dart';

@HiveType()
class PersonModel extends HiveObject{
@HiveField(0)
String language;

@HiveField(1)
String examName;

@HiveField(2)
int examId;

@HiveField(3)
Profile profile;

@HiveField(4)
ListExam listexam;

@override
String toString() {
return jsonEncode({
'language': this.language,
'examName': this.examName,
'examId': this.examId,
'profile': this.profile,
'listexam': this.listexam
});
}

PersonModel(
this.language, this.examName, this.examId, this.profile, this.listexam);
}

Profile类供引用:
class Profile {
String name='';
String lang='';
String emailId='';
String mobileNumber='';
String state='';
String city='';
String district='';
String pinCode='';
String profilePic='';
Profile(
this.name,
this.lang,
this.emailId,
this.mobileNumber,
this.district,
this.state,
this.city,
this.pinCode,this.profilePic);

Profile.fromJson(Map<String, dynamic> json) {
// print('fromjson:$json');
if (json != null) {
name = json['name'];
lang = json['language'];
emailId = json['emailId'];
mobileNumber = json['mobileNumber'];
district = json['district'];
state = json['state'];
city = json['city'];
pinCode = json['pinCode'];
profilePic=json['profilePic'];
} else {
print('in else profile from json');
// name = '';
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['language'] = this.lang;
data['emailId'] = this.emailId;
data['mobileNumber'] = this.mobileNumber;
data['district'] = this.district;
data['state'] = this.state;
data['city'] = this.city;
data['pinCode'] = this.pinCode;
data['profilePic']=this.profilePic;
return data;
}
}

当我尝试像这样添加添加的对象时发生错误
item.add(PersonModel(label, null, null, Profile(), ListExam())); // label='English'

以下是发生的错误:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: HiveError: Cannot write, unknown type: Profile. Did you forget to register an adapter?



现在,有人可以指出我在这里做错了什么吗?

最佳答案

Hive 插件默认支持原语、列表和映射。要将它用于您自己的 Dart 对象,您需要生成一个 TypeAdapter。

您需要为您拥有的每个对象创建一个 TypeAdapter。所以你需要为 应用相同的东西公司简介 您为 所做的类(class)人物模型

part 'profile.g.dart';

@HiveType()
class Profile {
@HiveField(0)
String name;
@HiveField(1)
String lang;
@HiveField(2)
String emailId;
@HiveField(3)
String mobileNumber;
@HiveField(4)
String state;
@HiveField(5)
String city;
@HiveField(6)
String district;
@HiveField(7)
String pinCode;
@HiveField(8)
String profilePic;

Profile(
this.name,
this.lang,
this.emailId,
this.mobileNumber,
this.district,
this.state,
this.city,
this.pinCode,this.profilePic);


Profile.fromJson(Map<String, dynamic> json) {
// print('fromjson:$json');
if (json != null) {
name = json['name'];
lang = json['language'];
emailId = json['emailId'];
mobileNumber = json['mobileNumber'];
district = json['district'];
state = json['state'];
city = json['city'];
pinCode = json['pinCode'];
profilePic=json['profilePic'];
} else {
print('in else profile from json');
// name = '';
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['language'] = this.lang;
data['emailId'] = this.emailId;
data['mobileNumber'] = this.mobileNumber;
data['district'] = this.district;
data['state'] = this.state;
data['city'] = this.city;
data['pinCode'] = this.pinCode;
data['profilePic']=this.profilePic;
return data;
}

}

如果您使用的是最新的配置单元版本,您还需要为您的 HiveType 提供一个类型 ID,例如
@HiveType(typeId: 0)

我看到您还有另一个名为 的自定义类列表考试 .您还需要为该类(class)做同样的事情

关于flutter - 如何在 hive 对象中编写自定义类对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59717182/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com