我有一个模型,该模型包含其他模型的列表作为属性,我正在从用户填写的表单中动态收集该模型。 第一个模型的代码:
class Attraction {
String atName;
String atDiscreption;
double latitude;
double longitude;
GeoPoint geoPoint;
String atImageUrl;
List<Timeline> timlines;
Attraction(
{
this.atName,
this.atDiscreption,
this.geoPoint,
this.latitude,
this.longitude,
this.atImageUrl,
this.timlines});
}
第二个模型的代码:
class Timeline {
int date;
String descreption;
Timeline(this.date, this.descreption);
}
我正在使用此方法将第一个模型写到Firestore Firebase中:
final CollectionReference attractions =
Firestore.instance.collection('attractions');
Future updateAttractionData(Attraction attraction) async {
return await attractions.document().setData({
'atName': attraction.atName,
'atDiscreption': attraction.atDiscreption,
'atImageUrl': attraction.atImageUrl,
'location': attraction.geoPoint,
});
}
并且我想将时间轴列表作为子集合写入Firebase? 我尝试了这种方法,但是没有用:
Future updateTimelinesData(Attraction attraction) async {
return attraction.timlines.map((e) =>
attractions.document().collection('timelines').document().setData({
'date': e.date,
'discreption': e.descreption,
}));
}
PS:我可以将数据作为地图字段直接传递给文档,如以下代码所示:
final CollectionReference attractions =
Firestore.instance.collection('attractions');
Future updateAttractionData(Attraction attraction) async {
List<Map> convertTimlinesToMap({List<Timeline> timlines}) {
List<Map> timelines = [];
timlines.forEach((Timeline timeline) {
Map time = timeline.toMap();
timelines.add(time);
});
return timelines;
}
return await attractions.document().setData({
'atName': attraction.atName,
'atDiscreption': attraction.atDiscreption,
'atImageUrl': attraction.atImageUrl,
'location': attraction.geoPoint,
//we used a method here because Firebase does not writes list of objects , but it can do map attributes
//so we're converting the list of timeline objects to list of maps
'timeline': convertTimlinesToMap(timlines: attraction.timlines),
});
}
但是我正在寻找的是将其作为子集合而不是作为字段传递。