从嵌套列表创建子集合



我创建了一个保存食谱的flutter应用程序。

每个食谱都有配料表。

我使用配方模型创建配方,里面有一个配料列表。

默认情况下,当我将上述配方模型保存到Firebase Firestore时,配料列表将保存为数组。

我计划随着时间的推移扩展这些概念,并希望将这些成分存储在一个子集合中。

很明显,我能够迭代这些问题,并将它们作为文档添加到自己的集合中,但这似乎很混乱,将来可能会给我带来问题。

有没有一种方法可以指定将子模型创建为子集合而不是数组?

下面是我目前用来写数据的

class FirestoreService {
FirestoreServiceNew._();
static final instance = FirestoreServiceNew._();
Future<void> setData({
@required String path,
@required Map<String, dynamic> data,
bool mergeBool = false,
}) async {
try {
final reference = FirebaseFirestore.instance.doc(path);
print('$path: $data');
await reference.set(data);
} catch (e) {
print('error: $e');
}
}
Future<void> bulkSet({
@required String path,
@required List<Map<String, dynamic>> datas,
bool merge = false,
}) async {
final reference = FirebaseFirestore.instance.doc(path);
final batchSet = FirebaseFirestore.instance.batch();
print('$path: $datas');
}
Future<void> deleteData({@required String path}) async {
final reference = FirebaseFirestore.instance.doc(path);
print('delete: $path');
await reference.delete();
}
Stream<List<T>> collectionStream<T>({
@required String path,
@required T builder(Map<String, dynamic> data, String documentID),
Query queryBuilder(Query query),
int sort(T lhs, T rhs),
}) {
Query query = FirebaseFirestore.instance.collection(path);
if (queryBuilder != null) {
query = queryBuilder(query);
}
final Stream<QuerySnapshot> snapshots = query.snapshots();
return snapshots.map((snapshot) {
final result = snapshot.docs
.map((snapshot) => builder(snapshot.data(), snapshot.id))
.where((value) => value != null)
.toList();
if (sort != null) {
result.sort(sort);
}
return result;
});
}
Stream<List<T>> requestStream<T>({
@required String path,
@required T builder(Map<String, dynamic> data, String documentID),
Query queryBuilder(Query query),
int sort(T lhs, T rhs),
}) {
Query query = FirebaseFirestore.instance.collection(path);
if (queryBuilder != null) {
query = queryBuilder(query);
}
final Stream<QuerySnapshot> snapshots = query.snapshots();
return snapshots.map((snapshot) {
final result = snapshot.docs
.map((snapshot) => builder(snapshot.data(), snapshot.id))
.where((value) => value != null)
.toList();
if (sort != null) {
result.sort(sort);
}
return result;
});
}
Stream<T> documentStream<T>({
@required String path,
@required T builder(Map<String, dynamic> data, String documentID),
}) {
final DocumentReference reference = FirebaseFirestore.instance.doc(path);
final Stream<DocumentSnapshot> snapshots = reference.snapshots();
return snapshots.map((snapshot) => builder(snapshot.data(), snapshot.id));
}
}

和我的型号

class Recipe {
String id;
List<Ingredient> ingredients;
String title;
String description;
Timestamp createdOn;
Recipe(
{this.id = '',
this.ingredients,
this.title,
this.description,
this.createdOn});
Recipe.fromData(Map<String, dynamic> json, String docid) {
id = docid ?? '';
title = json['title'];
description = json['description'];
createdOn = json['createdOn'];
if (json['questions'] != null) {
ingredients= new List<Ingredients>();
json['ingredients'].forEach((v) {
questions.add(new Ingredients.fromData(v, null));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['description'] = this.description;
data['title'] = this.title;
data['createdOn'] = this.createdOn;
if (this.ingredients != null) {
data['ingredients'] = this.ingredients.map((v) => v.toJson()).toList();
}
return data;
}
}

您可以通过交替调用collection((和doc((来构建嵌套在子集合中的文档的路径,这两个函数分别返回CollectionReference和DocumentReference对象。

FirebaseFirestore.instance
.collection("top-level-collection")
.doc("document-id-1")
.collection("nested-subcollection")
.doc("document-id-2")

最新更新