Reputation: 9
Future<List> readTable(List<Date> dates, List<Plot> plots) async {
List rows = [];
await userCollection.doc(uid).collection('plots').get().then(
(QuerySnapshot snapshot) => {
// ignore: avoid_function_literals_in_foreach_calls
snapshot.docs.forEach(
(plot) async {
List row = [];
for (Date d in dates) {
List works = [];
print(firebaseDocumentDate(d.dateTime));
await userCollection
.doc(uid)
.collection('plots')
.doc(plot['name'])
.collection(firebaseDocumentDate(d.dateTime))
.get()
.then(
(QuerySnapshot date) {
for (var work in date.docs) {
works.add(
Work(
work.id,
work['category'],
work['worker'],
work['description'],
work['status'],
),
);
}
},
);
row.add(works);
}
rows.add(row);
print(rows);
},
),
},
);
return rows;
}
The "Date" class
class Date {
final DateTime dateTime;
final String text;
Date(this.dateTime, this.text);
}
The "Work" class
class Work {
final String id;
final String category;
final String worker;
final String description;
final String status;
Work(
this.id,
this.category,
this.worker,
this.description,
this.status,
);
}
A diagramatic representation of my database
The output of the print statement.
flutter: [[[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [Instance of 'Work'], [], [], [Instance of 'Work'], [], []], [[], [], [], [], [], [], []]]
But when I print the final output it is an empty list.
flutter: []
I have worked with a similar function on the same project.
Future<List<Plot>> readPlots() async {
List<Plot> plots = [];
await userCollection.doc(uid).collection('plots').get().then(
(QuerySnapshot snapshot) => {
snapshot.docs.forEach(
(plot) {
plots.add(
Plot(plot["name"]),
);
},
)
},
);
return plots;
}
This works well. I think the await function is so lengthy that flutter simply returns the empty list I declared in the function.
I was expecting the output that that the print statement inside the function returns.
flutter: [[[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [], [], [], [], [], []], [[], [Instance of 'Work'], [], [], [Instance of 'Work'], [], []], [[], [], [], [], [], [], []]]
Upvotes: 0
Views: 45
Reputation: 9
Fixed it myself by taking in a list of plots from another function and running it over with a for loop instead of fetching it within the function.
Future<List> readTable(List<Date> dates, List<Plot> plots) async {
List rows = [];
for (Plot plot in plots) {
List row = [];
for (Date d in dates) {
await userCollection
.doc(uid)
.collection('plots')
.doc(plot.name)
.collection(firebaseDocumentDate(d.dateTime))
.get()
.then(
(QuerySnapshot date) {
List works = [];
for (var work in date.docs) {
works.add(
Work(
work.id,
work['category'],
work['worker'],
work['description'],
work['status'],
),
);
}
row.add(works);
},
);
}
rows.add({'rowName': plot.name, 'cells': row});
}
return rows;
}
Upvotes: 0