flutter - Fetch all documents and their sub-collections
问题描述
I am coding an employee scheduling app in Flutter and I am using Firestore to store my data.
I have a collection of Employees in which there are saved basic employee parameters such as name, salary, designation, email.
For each employee document, I have a sub-collection containing their unavailability. The unavailability might be due to being already assigned to a shift, sickness, vacation.
For fetching the data, I implemented a firebase_employees_repository. Within the repository, I have the following function that gets the employees in realtime.
@override
Stream<List<Employee>> employees() {
return employeeCollection.snapshots().map((snapshot) {
return snapshot.documents
.map((doc) => Employee.fromEntity(EmployeeEntity.fromSnapshot(doc)))
.toList();
});
}
Now I want to do the same with the unavailabilities.
As far as my understanding, the unavailability list of an employee should get saved within the employee object.
I need the unavailability list of an employee, to show its status for a day/week/month. Also, when creating a new shift and wanting to assign to an employee, only the available should be shown.
This is what could be a solution, but I am getting the following error:
A value of type 'Future< List< Iterable< Unavailability>>>' can't be assigned to a variable of type 'Stream< List< Unavailability>>'. Try changing the type of the variable, or casting the right-hand type to 'Stream< List< Unavailability>>'.
@override
Stream<List<Employee>> employees() {
return employeeCollection.snapshots().map((snapshot) {
return snapshot.documents
.map((doc) {
Stream<List<Unavailability>> unavailabilities =
employeeCollection
.document(doc.documentID)
.collection('Unavailabilities')
.snapshots().map((snapshot) {
return snapshot.documents
.map((innerDoc) => Unavailability.fromEntity(UnavailabilityEntity.fromSnapshot(innerDoc)));
}).toList()
;
return Employee.fromEntity(EmployeeEntity.fromSnapshot(doc));})
.toList();
});
}
The whole project can be found in Github under this link: schedulingapp
解决方案
你所期望的和你返回的不是同一种类型。我假设你想接收一个流是Stream<List<Unavailability>
. 在这种情况下,您可以使用StreamTransformer
从 Firestore 更改流的类型,这样您就可以获得所需的内容。但是,我不知道您的方法是否有效,因为您不在unavailabilities
任何地方使用。此外,我不知道UnavailabilityEntity.fromSnapshot
是为了什么。假设您尝试解析数据。尝试在下面使用此代码。至少错误应该消失。
Stream<List<Unavailability>> unavailabilities = employeeCollection
.document(doc.documentID)
.collection('Unavailabilities')
.snapshots()
.transform<List<Unavailability>>(
StreamTransformer.fromHandlers(
handleData: (QuerySnapshot data, EventSink<List<Unavailability>> sink) {
sink.add(
data.documents.map(
(innerDoc) => Unavailability.fromEntity(
UnavailabilityEntity.fromSnapshot(innerDoc),
),
).toList(),
);
},
),
);
推荐阅读
- python - 遍历数据框以提取特定时间段的增量
- r - 从 ggplot2 主题中提取属性(即字体)
- java - 来自 oracle 文档的流类型的 JDBC 提取大小重置为 1
- python - TF 2.0 LSTM:如何用于支出预测?
- jenkins - Testcafe 在构建的 Jenkins 上失败
- swift - 通知内容扩展标题高度
- python - 如何比较 2 个不同的 csv 文件并输出差异
- javascript - 我可以不使用 forEach() 而是使用 .reduce() 来使这个函数更干净吗?
- jenkins-groovy - Jenkins当前上下文中的惰性GString
- html - Html 项目符号未按预期出现