【发布时间】:2020-01-13 13:01:55
【问题描述】:
在颤振中使用 hive 时,我在 hive 对象中使用了我的自定义类对象 Profile profile。
所以,最初,我将自定义类对象(Profile profile)设置为null,同时添加到 Hive 框中。
以下是我的 Hive 课程:
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)] 未处理的异常:HiveError:无法写入,未知类型:配置文件。您忘记注册适配器了吗?
现在,有人可以指出我在这里做错了什么吗?
【问题讨论】:
标签: flutter