database - 尝试使用 Flutter 中的 Dart 从 Firestore 集合中的文档访问和打印数据时出现错误状态错误?
问题描述
这是错误:
引发了以下断言:_MapStream<QuerySnapshot<Map<String, dynamic>> 引发异常,List<WeightliftingLogs?>> 被监听
StreamProvider<List<WeightliftingLogs?>>,但没有catchError
提供。
异常:错误状态:DocumentSnapshotPlatform 中不存在字段
这是我的代码:
final CollectionReference usersCollection = FirebaseFirestore.instance.collection('users');
var wLCollection = FirebaseFirestore.instance.collectionGroup('Weightlifting');
List<WeightliftingLogs?> _WLlistFromSnapshot(QuerySnapshot snapshot){
return snapshot.docs.map((doc){
return WeightliftingLogs(
date: doc.get('date') ?? Timestamp.now(),
deadLiftWeight: doc.get('deadLiftWeight') ?? 0,
deadLiftReps: doc.get('deadLiftReps') ?? 0,
backSquatWeight: doc.get('backSquatWeight') ?? 0,
backSquatReps: doc.get('backSquatReps') ?? 0,
hipThrustWeight: doc.get('hipThrustWeight') ?? 0,
hipThrustReps: doc.get('hipThrustReps') ?? 0,
legPressWeight: doc.get('legPressWeight') ?? 0,
legPressReps: doc.get('legPressReps') ?? 0,
benchPressWeight: doc.get('benchPressWeight') ?? 0,
benchPressReps: doc.get('benchPressReps') ?? 0,
lateralPulldownWeight: doc.get('lateralPulldownWeight') ?? 0,
lateralPulldownReps: doc.get('lateralPulldownReps') ?? 0,
bicepCurlWeight: doc.get('bicepCurlWeight') ?? 0,
bicepCurlReps: doc.get('bicepCurlReps') ?? 0,
tricepExtensionWeight: doc.get('tricepExtensionWeight') ?? 0,
tricepExtensionReps: doc.get('tricepExtensionReps') ?? 0
);
}).toList();
}
Stream<List<WeightliftingLogs?>> get WLlogs {
return wLCollection.snapshots()
.map(_WLlistFromSnapshot);
}
这是用户注册时创建新的举重日志时的代码:
Future newWeightliftingLog(Timestamp date, int deadLiftWeight, int deadLiftReps, int backSquatWeight, int backSquatReps, int hipThrustWeight, int hipThrustReps, int legPressWeight, int legPressReps, int benchPressWeight, int benchPressReps, int lateralPulldownWeight, int lateralPulldownReps, int bicepCurlWeight, int bicepCurlReps, int tricepExtensionWeight, int tricepExtensionReps) async {
return await usersCollection.doc(uid).collection('Weightlifting').doc().set({
'date': date,
'deadLiftWeight': deadLiftWeight,
'deadLiftReps': deadLiftReps,
'backSquatWeight': backSquatWeight,
'backSquatReps': backSquatReps,
'hipThrustWeight': hipThrustWeight,
'hipThrustReps': hipThrustReps,
'legPressWeight': legPressWeight,
'legPressReps': legPressReps,
'benchPressWeight': benchPressWeight,
'benchPressReps': benchPressReps,
'lateralPulldownWeight': lateralPulldownWeight,
'lateralPulldownReps': lateralPulldownReps,
'bicepCurlWeight': bicepCurlWeight,
'bicepCurlReps': bicepCurlReps,
'tricepExtensionWeight': tricepExtensionWeight,
'tricepExtensionReps': tricepExtensionReps,
});
}
这是我的举重日志类:
class WeightliftingLogs {
final Timestamp date;
final int deadLiftWeight;
final int deadLiftReps;
final int backSquatWeight;
final int backSquatReps;
final int hipThrustWeight
final int hipThrustReps;
final int legPressWeight;
final int legPressReps;
final int benchPressWeight;
final int benchPressReps;
final int lateralPulldownWeight;
final int lateralPulldownReps;
final int bicepCurlWeight;
final int bicepCurlReps;
final int tricepExtensionWeight;
final int tricepExtensionReps;
WeightliftingLogs ({
required this.date,
required this.deadLiftWeight, required this.deadLiftReps, required this.backSquatWeight, required this.backSquatReps, required this.hipThrustWeight, required this.hipThrustReps, required this.legPressWeight, required this.legPressReps, required this.benchPressWeight, required this.benchPressReps, required this.lateralPulldownWeight, required this.lateralPulldownReps, required this.bicepCurlWeight, required this.bicepCurlReps, required this.tricepExtensionWeight, required this.tricepExtensionReps, }); }
这是我打印它们的地方:
final logs = Provider.of<List<WeightliftingLogs?>>(context);
logs.forEach((weightliftinglog){
print(weightliftinglog!.date);
print(weightliftinglog.deadLiftWeight);
print(weightliftinglog.deadLiftReps);
print(weightliftinglog.backSquatWeight);
print(weightliftinglog.backSquatReps);
print(weightliftinglog.hipThrustWeight);
print(weightliftinglog.hipThrustReps);
print(weightliftinglog.legPressWeight);
print(weightliftinglog.legPressReps);
print(weightliftinglog.benchPressWeight);
print(weightliftinglog.benchPressReps);
print(weightliftinglog.lateralPulldownWeight);
print(weightliftinglog.lateralPulldownReps);
print(weightliftinglog.bicepCurlWeight);
print(weightliftinglog.bicepCurlReps);
print(weightliftinglog.tricepExtensionWeight);
print(weightliftinglog.tricepExtensionReps);
});
这是我在用户注册时为所有数据字段初始化默认值的方法:
final Timestamp now = Timestamp.now();
await DatabaseService(uid: user!.uid).newWeightliftingLog(now, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0);
解决方案
推荐阅读
- php - 如何在操作另一个集合的同时遍历集合中的每条记录?
- c++ - VSCode 和 Windows 10 的 cmake 错误:没有这样的文件或目录
- python - TypeError: partitions must be TopicPartition namedtuples
- haskell - 是否可以将嵌套列表返回为 IO()
- java - 如何将对象发送到接收 Java 对象 (Angular) 的 GET 端点?
- java - 在 Android 中使用 NavigationEngine.navigate 时如何处理错误
- java - 使用服务帐户而不是 O Auth Client ID 设置的 Gmail API
- python - 从与条件匹配的列表创建字典
- java - Spring - 返回端点的字符串结果并将其存储在变量中
- arrays - 二维字符串数组的复合文字