// getActivityFeed() function
class _ActivityFeedState extends State<ActivityFeed> {
Future<List<ActivityFeedItem>> getActivityFeed() async {
QuerySnapshot snapshot = await activityFeedRef
.doc(currentUser.id)
.collection('feedItems')
.orderBy('timestamp', descending: true)
.limit(50)
.get();
print(snapshot.docs);
snapshot.docs.forEach((doc) {
print(doc.data());
List<ActivityFeedItem> feedItems = [];
feedItems.add(ActivityFeedItem.fromDocument(doc));
print(feedItems);
// print('Activity Feed Item: ${doc.data}');
});
print("feed2: $feedItems");
return feedItems;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.orange,
appBar: header(context, titleText: "Activity Feed"),
body: Container(
child: FutureBuilder(
future: getActivityFeed(),
builder: (context, snapshot) {
print("hey$feedItems");
if (!snapshot.hasData) {
return circularProgress();
}
return ListView(
children: snapshot.data,
);
},
)),
);
}
}
//FutureBuilder
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.orange,
appBar: header(context,
titleText: "Activity Feed",
),
body: Container(
child: FutureBuilder(
future: getActivityFeed(),
builder: (context, snapshot) {
print("hey$feedItems");
if (!snapshot.hasData) {
return circularProgress();
}
return ListView(
children: snapshot.data,
);
},
),
),
);
}
}
<块引用>
我没有得到快照数据....快照数据为空并且 未来的建造者正在返回给我 circleProgess(..... 我有 定义了函数 getActivityFeed,这是一个未来。我可以看到 调试控制台有快照数据,可以看到snapshot.doc 控制台中的数据但不知何故 print(doc.data()) 之后的代码不是 执行并打印(feedItems)我在控制台中看不到它。我是什么 代码做错了吗?
答案 0 :(得分:1)
您正在为每个初始化列表。
snapshot.docs.forEach((doc) {
List<ActivityFeedItem> feedItems = []; // This line
feedItems.add(ActivityFeedItem.fromDocument(doc));
});
将此行移到 forEach 之外
List<ActivityFeedItem> feedItems = [];
snapshot.docs.forEach((doc) {
feedItems.add(ActivityFeedItem.fromDocument(doc));
});