作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在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);
}
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(typeId: 0)
关于flutter - 如何在 hive 对象中编写自定义类对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59717182/
我是一名优秀的程序员,十分优秀!