我在类映射方面有一个小问题...我在flutter应用程序中使用firecloud(NoSql DB),但是在检索数据时遇到很多问题!
这是我的会议课程,其中包含所有数据,我可以获取所有数据,但是List _Tags和List _Users出现一些问题。我该如何映射并正确获取这些数据??
class Meeting{
userIcon newUserIcon;
User newUser;
String newtag;
String _ID;
String _Name;
String _Date;
String _Position;
String _Image;
String _Hour;
List<String> _Tags;
List<userIcon> _Users;
String _Description;
User _Manager;
bool _Favorite;
Meeting(this._ID,this._Name,this._Date,this._Position,this._Image,this._Hour,this._Tags,this._Users,this._Description,this._Manager,this._Favorite);
Meeting.fromMap(Map<String, dynamic> map){
this._ID = map['ID'];
this._Name = map['nome'];
this._Date= map['data'];
this._Position= map['posizione'];
this._Image= map['immagine'];
this._Hour= map['data'];
this._Tags= map['tags'].cast<String>(); //creo la lista di scringhe
this._Users= map['id_partecipanti'].forEach((element){
newUserIcon = new userIcon.fromMap(element);
});
this._Description = map['descrizione'];
this._Manager = new User.fromMap(map['id_organizzatore']);
this._Favorite = map['preferito'];
}
Meeting.map(dynamic obj) {
this._ID = obj['ID'];
this._Name = obj['Name'];
this._Date= obj['Date'];
this._Position= obj['Position'];
this._Image= obj['Image'];
this._Hour= obj['Hour'];
this._Tags= obj['Tags'];
this._Users= obj['Users'];
this._Description = obj['Description'];
this._Manager = obj['Manager'];
this._Favorite = obj['Favorite'];
}
String get ID => this._ID;
String get Name => _Name;
String get Date => _Date;
String get Position=> _Position;
String get Image => _Image;
String get Hour => _Hour;
List<String> get Tags => _Tags;
List<userIcon> get Users=> _Users;
String get Description => _Description;
User get Manager => _Manager;
bool get Favorite => _Favorite;
Map<String, dynamic> toMap() {
var map =new Map<String, dynamic>();
if(ID != null){
map["ID"] = ID;
}
map["ID"] = ID;
map["Name"] = Name;
map["Date"] = Date;
map["Position"] = Position;
map["Image"] = Image;
map["Hour"] = Hour;
map["Tags"] = Tags;
map["Users"] = Users;
map["Description"] = Description;
map["Manager"] = Manager;
map["Favorite"] = Favorite;
return map;
}
}
这是userIcon类
class userIcon{
String _ID;
String _immagine;
int _eta;
userIcon(this._ID,this._immagine,this._eta);
userIcon.map(dynamic obj) {
this._ID = obj['ID'];
this._immagine = obj['immagine'];
this._eta = obj['eta'];
}
String get ID => this._ID;
String get immagine => this._immagine;
int get eta => this._eta;
Map<String, dynamic> toMap() {
var map =new Map<String, dynamic>();
if(ID != null){
map["ID"] = ID;
}
map["immagine"] = immagine;
map["eta"] = eta;
return map;
}
userIcon.fromMap(Map<String, dynamic> map){
this._ID = map['ID'];
this._immagine=map["immagine"];
this._eta=map["eta"];
}
}
这是Firecloud查询/ map:
Future<List<Meeting>> getData() async {
List<Meeting> _incontri = [];
List<DocumentSnapshot> _temp =[];
Query q = databaseReference.collection("incontri").limit(10);
QuerySnapshot querySnapshot = await q.getDocuments();
_temp = querySnapshot.documents;
_incontri = _temp.map(
(doc) => Meeting.fromMap(doc.data)).toList();
return _incontri;
}
我找不到有关此问题的任何文档或任何其他有关我的问题的文章...有人可以帮助我:)吗?如果您知道一些文章,我可以在其中阅读有关地图的知识并学到一些东西,我将不胜感激! 谢谢!
答案 0 :(得分:0)
Meeting._Users未被填充,因为您使用的是List.forEach,但未返回列表,因此应使用List.map,然后使用{将返回的Iterable转换为列表{3}}。
您正在使用toList进行某种类型的转换并获得Meeting._Tags。根据您提供的信息,很难说出问题是map['tags']
是不是正确的类型,还是遇到其他麻烦。
如果您不想手动处理JSON <->模型转换,建议使用List.cast之类的代码生成工具。这里json_serializable关于使用json_serializable。