流和 firebase 的问题错误状态:DocumentSnapshotPlatform 中不存在字段
Problem with stream and firebase Bad state: field does not exist within the DocumentSnapshotPlatform
我有一个 StreamBuilder,旨在根据传递的变量从 firestore 获取文档列表,但我遇到了上述错误。
StreamBuilder
StreamBuilder<QuerySnapshot>(
stream: DatabaseService.getFeedPosts(widget.currentUserId),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
// Loader Animation Widget
Padding(padding: const EdgeInsets.only(top: 20.0)),
Text('Finding tasks'),
],
),
);
}
if (snapshot.hasData) {
final documents = snapshot.data.docs;
return ListView(
children: documents
.map(
(doc) => TaskList(
currentUserId: widget.currentUserId,
task: doc['id'],
user: doc['ownerId'],
),
)
.toList());
} else if (snapshot.hasError) {
print('Error');
return Text('It\'s Error!');
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Unable to find any tasks'),
],
),
);
}),
Firebase 查询
static Stream<QuerySnapshot> getFeedPosts(String userId) {
return tasksRef
.where('authorId', isEqualTo: userId)
.orderBy('timestamp', descending: true)
.snapshots();
}
您应该在使用 []
运算符之前调用 data()
method from QueryDocumentSnapshot
:
return ListView(
children: documents
.map(
(doc) => TaskList(
currentUserId: widget.currentUserId,
task: doc.data()['id'],
user: doc.data()['ownerId'],
),
)
.toList(),
);
我有一个 StreamBuilder,旨在根据传递的变量从 firestore 获取文档列表,但我遇到了上述错误。
StreamBuilder
StreamBuilder<QuerySnapshot>(
stream: DatabaseService.getFeedPosts(widget.currentUserId),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(),
// Loader Animation Widget
Padding(padding: const EdgeInsets.only(top: 20.0)),
Text('Finding tasks'),
],
),
);
}
if (snapshot.hasData) {
final documents = snapshot.data.docs;
return ListView(
children: documents
.map(
(doc) => TaskList(
currentUserId: widget.currentUserId,
task: doc['id'],
user: doc['ownerId'],
),
)
.toList());
} else if (snapshot.hasError) {
print('Error');
return Text('It\'s Error!');
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Unable to find any tasks'),
],
),
);
}),
Firebase 查询
static Stream<QuerySnapshot> getFeedPosts(String userId) {
return tasksRef
.where('authorId', isEqualTo: userId)
.orderBy('timestamp', descending: true)
.snapshots();
}
您应该在使用 []
运算符之前调用 data()
method from QueryDocumentSnapshot
:
return ListView(
children: documents
.map(
(doc) => TaskList(
currentUserId: widget.currentUserId,
task: doc.data()['id'],
user: doc.data()['ownerId'],
),
)
.toList(),
);