0
 // 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,
      );
       },
     ),
     ),
     );
   }
   }

I am not getting snapshot data.... the snapshot data is null and the future builder is returning me the circularProgess()..... I have defined the function getActivityFeed which is a future. I can see in debug console that there is snapshot data and can see snapshot.doc data in console but somehow the code after print(doc.data()) is not executed and print(feedItems) I can't see it in console. What I am doing wrong in the code?

1 Answer 1

1

You are initializing the list inside for each.

snapshot.docs.forEach((doc) {
    List<ActivityFeedItem> feedItems = []; // This line
    feedItems.add(ActivityFeedItem.fromDocument(doc));
});

Move this line outside forEach

List<ActivityFeedItem> feedItems = [];
snapshot.docs.forEach((doc) {
    feedItems.add(ActivityFeedItem.fromDocument(doc));
});
1
  • Before I wrote this code I did the same thing, my feedItems was outside of forEach block but still I am not getting snapshot data but getting the circularProgress() ...... Commented Jul 5, 2021 at 7:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.