【发布时间】:2021-06-03 18:30:46
【问题描述】:
我更新了以下软件包:
- firebase_core:从 ^0.5.0+1 到 ^1.2.0
- cloud_firestore:从 0.14.1+3 到 ^2.2.0
- firebase_auth:从 ^0.18.0+1 到 ^1.2.0
更新软件包后,我收到以下错误:
═══════ 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'];
}
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore