我有一个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'),
],
),
);
}),
重火力点查询static Stream<QuerySnapshot> getFeedPosts(String userId) {
return tasksRef
.where('authorId', isEqualTo: userId)
.orderBy('timestamp', descending: true)
.snapshots();
}
在使用[]
运算符之前,应该先从QueryDocumentSnapshot
调用data()
方法:
return ListView(
children: documents
.map(
(doc) => TaskList(
currentUserId: widget.currentUserId,
task: doc.data()['id'],
user: doc.data()['ownerId'],
),
)
.toList(),
);