我更新了以下软件包:
更新软件包后,我收到以下错误:
═══════ Exception caught by widgets library ═══════════════════════════════════
The following StateError was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#77e63):
Bad state: field does not exist within the DocumentSnapshotPlatform
The relevant error-causing widget was
StreamBuilder<QuerySnapshot<Object?>>
lib/screens/app_home.dart:183
When the exception was thrown, this was the stack
#0 DocumentSnapshotPlatform.get._findKeyValueInMap
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:86
#1 DocumentSnapshotPlatform.get._findComponent
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:104
#2 DocumentSnapshotPlatform.get
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:120
#3 _JsonDocumentSnapshot.get
package:cloud_firestore/src/document_snapshot.dart:92
#4 _JsonDocumentSnapshot.[]
package:cloud_firestore/src/document_snapshot.dart:96
...
════════════════════════════════════════════════════════════════════════════════
下面是flutter doctor
的输出:
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.3, on macOS 11.3.1 20E241 darwin-x64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.56.2)
[✓] Connected device (3 available)
• No issues found!
以下是与错误相关的代码:
final coursesCollection = FirebaseFirestore.instance.collection('courses').limit(10).where('courseLive', isEqualTo: true);
.
.
.
return StreamBuilder<QuerySnapshot>(
stream: coursesCollection.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: kAccentColor,
),
);
}
final courseSnapshot = snapshot.data.docs;
List<CourseTile> courseTiles = [];
CourseData course;
for (var courseData in courseSnapshot) {
course = CourseData.fromDocumentSnapshot(snapshot: courseData);
final courseDocID = course.courseDocID;
final courseID = course.courseID;
final courseTitle = course.courseTitle;
final courseDescription = course.courseDescription;
final courseSubTitle = course.courseSubTitle;
final courseBadge = course.courseBadge;
final courseLevel = course.courseLevel;
final coursePaid = course.coursePaid;
final courseImage = course.courseImage;
final courseBgColor = hexToColor(course.courseBackgroundColor);
final courseBgColor1 = hexToColor(course.courseBgColor1);
final courseBgColor2 = hexToColor(course.courseBgColor2);
final courseFgColor = hexToColor(course.courseFgColor);
final courseDeliveryFormat = course.courseDeliveryFormat;
final courseLive = course.courseLive;
final courseTile = CourseTile(
cardBackgroundColor: courseBgColor,
bgColor1: courseBgColor1,
bgColor2: courseBgColor2,
fgColor: courseFgColor,
cardImage: courseImage,
titleText: courseTitle,
titleTextColor: courseFgColor,
subTitleText: courseSubTitle,
courseID: courseID,
courseDocID: courseDocID,
);
courseTiles.add(courseTile);
}
return Expanded(
child: ListView(
children: courseTiles,
),
);
},
);
我确实验证了 firestore 文档中存在的字段,它们确实匹配。还尝试将软件包恢复到以前的版本,但它仍然会中断。
不确定,需要做些什么来解决这个问题。
任何帮助将不胜感激。
非常感谢您提前抽出时间。
Bad state: field does not exist
错误消失了。但是,我仍然收到以下错误。
════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#ef5a5):
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'DocumentSnapshot<Object>'
The relevant error-causing widget was
StreamBuilder<QuerySnapshot<Object?>>
lib/screens/app_home.dart:183
When the exception was thrown, this was the stack
#0 CourseStream.build.<anonymous closure>
lib/screens/app_home.dart:200
#1 StreamBuilder.build
package:flutter/…/widgets/async.dart:545
#2 _StreamBuilderBaseState.build
package:flutter/…/widgets/async.dart:124
#3 StatefulElement.build
package:flutter/…/widgets/framework.dart:4612
#4 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4495
...
════════════════════════════════════════════════════════════════════════════════
下面是 CourseData
类中的方法,它解析从文档快照接收的数据。
CourseData.fromDocumentSnapshot({DocumentSnapshot snapshot}) {
courseDocID = snapshot.id;
courseID = snapshot['courseID'];
courseTitle = snapshot['courseTitle'];
courseSubTitle = snapshot['courseSubTitle'];
courseDescription = snapshot['courseDescription'];
courseLevel = snapshot['courseLevel'];
courseBadge = snapshot['courseType'];
coursePaid = snapshot['coursePaid'];
courseCategories = snapshot['courseCategories'];
courseImage = snapshot['courseImage'];
courseBackgroundColor = snapshot['courseBackgroundColor'];
courseForegroundColor = snapshot['courseForegroundColor'];
courseBgColor1 = snapshot['courseBgColor1'];
courseBgColor2 = snapshot['courseBgColor2'];
courseFgColor = snapshot['courseFgColor'];
courseDeliveryFormat = snapshot['courseDeliveryFormat'];
courseLive = snapshot['courseLive'];
}
答案 0 :(得分:1)
为了从您的 DocumentSnapshot
获取数据,您需要在快照上调用 .data()
。
文档快照平台
包含从一个读取的数据 Firestore 数据库中的文档。
可以通过调用data()或者调用get()来获取数据 特定领域。
DocumentSnapshotPlatform class
解决方案:
您可以保留此行:
course = CourseData.fromDocumentSnapshot(snapshot: courseData);
并将 CourseData 代码更新为以下代码块:
CourseData.fromDocumentSnapshot({DocumentSnapshot snapshot}) {
Map<String, dynamic> snapshotData = snapshot.data();
courseDocID = snapshot.id;
courseID = snapshotData['courseID'];
courseTitle = snapshotData['courseTitle'];
courseSubTitle = snapshotData['courseSubTitle'];
courseDescription = snapshotData['courseDescription'];
courseLevel = snapshotData['courseLevel'];
courseBadge = snapshotData['courseType'];
coursePaid = snapshotData['coursePaid'];
courseCategories = snapshotData['courseCategories'];
courseImage = snapshotData['courseImage'];
courseBackgroundColor = snapshotData['courseBackgroundColor'];
courseForegroundColor = snapshotData['courseForegroundColor'];
courseBgColor1 = snapshotData['courseBgColor1'];
courseBgColor2 = snapshotData['courseBgColor2'];
courseFgColor = snapshotData['courseFgColor'];
courseDeliveryFormat = snapshotData['courseDeliveryFormat'];
courseLive = snapshotData['courseLive'];
}