【发布时间】:2022-01-07 01:16:03
【问题描述】:
_getDataRowsForCompletedExerciseSets(workoutId) async {
try {
List listOfExercises = await FirebaseFirestore.instance
.collection('users')
.doc(currentUser!.uid)
.collection("workouts")
.doc(workoutId)
.collection("exercises")
.get()
.then((snapShot) => snapShot.docs);
for (int i = 0; i < listOfExercises.length; i++) {
FirebaseFirestore.instance
.collection('users')
.doc(currentUser!.uid)
.collection("workouts")
.doc(workoutId)
.collection("exercises")
.doc(listOfExercises[i].doc.id.toString())
.collection("sets")
.snapshots()
.listen(_createListOfExerciseSets);
}
setState(() {
for (ExerciseSet set in listOfExerciseSets) {
//print(set.weight);
completedExerciseSetRows.add(DataRow(cells: [
DataCell(Text(setCount.toString())),
//const DataCell(VerticalDivider(thickness: 5)),
DataCell(Text(set.weight.toString())),
//const DataCell(VerticalDivider(thickness: 5)),
DataCell(Text(set.reps.toString())),
//const DataCell(VerticalDivider(thickness: 5)),
DataCell((isSetToFailure == true) ? Icon(Icons.check) : Icon(null))
//const DataCell(VerticalDivider(thickness: 5)),
]));
}
});
} on Exception catch (_, e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.toString()),
duration: Duration(milliseconds: 1000),
),
);
}
return Container();
}
_createListOfExerciseSets(QuerySnapshot snapshot) {
var docs = snapshot.docs;
for (var doc in docs) {
listOfExerciseSets.add(ExerciseSet.fromFireStore(doc));
}
print('EXERCISE SETS = ' + listOfExerciseSets.toString());
}
我有以下数据库结构:
users-->{id}-->workouts-->{id}-->exercises-->{id}-->sets-->{id}--fields
我正在尝试获取特定锻炼(id)的练习以及每个练习的集合(包括字段)。 listOfExercises 始终为空(0 项)。也没有任何错误显示。
【问题讨论】:
-
请也分享您的 Json 响应。
-
什么json响应?很抱歉没有 json 响应。
-
我已经为 _createListOfExerciseSets 添加了代码;虽然 listOfExercises 与 listOfExerciseSets 不同。
-
在
setState中添加一些调试prints,你会看到它们在prints之前在_createListOfExerciseSets中打印 - 这就是为什么你总是有空数据的原因 -
_createListOfExerciseSets 是在 _getDataRowsForCompletedExerciseSets 中调用 setState 之前调用的单独方法。如何在 _createListOfExerciseSets 中的 setState 之后添加打印?! _createListOfExerciseSets 中没有 setState。
标签: list flutter dart google-cloud-firestore