dart – Flutter, Posts from users I follow

I have codes that access the ids of the users I follow. I want to combine this with my posts on the homepage and see only posts from people I follow. What should I do about it? …………………………………….. ……………………………………………………………………………

This is my code where I access the id of the users I follow.

class _FollowCardState extends State<FollowCard> {
  List<dynamic> followList = []; // shouldn't use dynamic

  getdata() async {
    List<dynamic> followers = [];

    final currentUserSnapshot = await FirebaseFirestore.instance
        .collection('users')
        .doc(widget.uid)
        .get();

    // get followerIds
    List<String> followIds =
        List.from(currentUserSnapshot.data()!['following']);
    // loop through all ids and get associated user object by userID/followerID
    for (int i = 0; i < followIds.length; i++) {
      var followId = followIds[i];
      var data = await FirebaseFirestore.instance
          .collection('users')
          .doc(followId)
          .get();

      // push that data into the temp list variable as we are going
      // to use that in to setState
      followers.add(data);
    }
    followers = followers.toSet().toList();

    setState(() => followList = followers);
  }

  @override
  void initState() {
    super.initState();
    getdata();
  }

  @override
  Widget build(BuildContext context) {
    // use the listView builder to render the list of followers card
    return SingleChildScrollView(
      physics: const NeverScrollableScrollPhysics(),
      child: ListView.builder(
          primary: false,
          shrinkWrap: true,
          itemCount: followList.length,
          itemBuilder: (context, index) {
            var followerItem = followList[index];
            print('photoUrl');

            return _buildFollowersCard(followerItem['photoUrl'],
                followerItem['username'], followerItem['uid']);
          }),
    );

Here are the posts shared on my homepage.

class _PostCardState extends State<PostCard> {
  int commentLen = 0;

  @override
  void initState() {
    super.initState();
    getComments();
  }

  void getComments() async {
    try {
      QuerySnapshot snap = await FirebaseFirestore.instance
          .collection('posts')
          .doc(widget.snap['postId'])
          .collection('comments')
          .get();

      commentLen = snap.docs.length;
    } catch (e) {
      showSnackBar(e.toString(), context);
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    final User user = Provider.of<UserProvider>(context).getUser;

    return Container(
      color: mobileBackgroundColor,
      child: Card(
        child: Column(
          children: [
            //Header
            Container(
              padding: const EdgeInsets.symmetric(
                vertical: 4,
                horizontal: 16,
              ).copyWith(right: 0),
              child: Row(
                children: [
                  CircleAvatar(
                    radius: 16,
                    backgroundImage: NetworkImage(
                      widget.snap['porfImage'],
                    ),
                  ),
                  Expanded(
                    child: Padding(
                      padding: EdgeInsets.only(left: 8),
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            widget.snap['username'],
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 4),
                    child: Text(
                      DateFormat.yMd().format(
                        widget.snap['datePublished'].toDate(),
                      ),
                      style: const TextStyle(
                        fontSize: 16,
                        color: secondaryColor,
                      ),
                    ),
                  ),
                  user.uid == widget.snap['uid']
                      ? IconButton(
                          onPressed: () {
                            showDialog(
                                context: context,
                                builder: (context) => Dialog(
                                      child: ListView(
                                          padding: const EdgeInsets.symmetric(
                                            vertical: 16,
                                          ),
                                          shrinkWrap: true,
                                          children: ['Delete']
                                              .map(
                                                (e) => InkWell(
                                                  onTap: () async {
                                                    FirestoreMethods()
                                                        .deletePost(widget
                                                            .snap['postId']);
                                                    Navigator.of(context).pop();
                                                  },
                                                  child: Container(
                                                    padding: const EdgeInsets
                                                            .symmetric(
                                                        vertical: 12,
                                                        horizontal: 16),
                                                    child: Text(e),
                                                  ),
                                                ),
                                              )
                                              .toList()),
                                    ));
                          },
                          icon: Icon(Icons.more_vert))
                      : IconButton(
                          onPressed: () {}, icon: Icon(Icons.more_vert))
                ],
              ),
            ),
            Container(
              width: double.infinity,
              padding: const EdgeInsets.only(
                top: 8,
                left: 15,
              ),
              child: Padding(
                padding: const EdgeInsets.only(left: 10, right: 20),
                child: RichText(
                  text: TextSpan(
                    style: const TextStyle(color: primaryColor),
                    children: [
                      // TextSpan(
                      //   text: widget.snap['username'],
                      //   style: const TextStyle(
                      //     fontWeight: FontWeight.bold,
                      //   ),
                      // ),
                      TextSpan(
                        text: ' ${widget.snap['description']}',
                      ),
                    ],
                  ),
                ),
              ),
            ),
            // Image
            //  SizedBox(
            //   height: MediaQuery.of(context).size.height * 0.35,
            //   width: double.infinity,
            //   child: Image.network(
            //     widget.snap['postUrl'],
            //     fit: BoxFit.cover,
            //   ),
            // ),

            // Like
            Container(
              width: double.infinity,
              padding: const EdgeInsets.only(
                top: 8,
                left: 10,
              ),
              child: Row(
                children: [
                  LikeAnimation(
                    isAnimating: widget.snap['likes'].contains(user.uid),
                    smallLike: true,
                    child: IconButton(
                      onPressed: () async {
                        await FirestoreMethods().LikePost(
                          widget.snap['postId'],
                          user.uid,
                          widget.snap['likes'],
                        );
                      },
                      icon: widget.snap['likes'].contains(user.uid)
                          ? const Icon(
                              Icons.favorite,
                              color: Colors.red,
                            )
                          : const Icon(
                              Icons.favorite_border,
                            ),
                    ),
                  ),
                  Container(
                    child: DefaultTextStyle(
                      style: Theme.of(context).textTheme.subtitle2!.copyWith(
                            fontWeight: FontWeight.w800,
                          ),
                      child: Text(
                        '${widget.snap['likes'].length}',
                        style: Theme.of(context).textTheme.bodyText2,
                      ),
                    ),
                  ),
                  IconButton(
                    onPressed: () => Navigator.of(context).push(
                      MaterialPageRoute(
                        builder: (context) => CommentsScreen(
                          snap: widget.snap,
                        ),
                      ),
                    ),
                    icon: const Icon(
                      Icons.comment_rounded,
                    ),
                  ),
                  InkWell(
                    onTap: () => Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => CommentsScreen(
                        snap: widget.snap,
                      ),
                    )),
                    child: Container(
                      padding: EdgeInsets.only(top: 4),
                      child: Text(
                        '$commentLen',
                        style: const TextStyle(
                          fontSize: 16,
                          color: secondaryColor,
                        ),
                      ),
                    ),
                  ),
                  // IconButton(
                  //   onPressed: () {},
                  //   icon: const Icon(
                  //     Icons.send,
                  //   ),
                  // ),
                  // Expanded(
                  //   child: Align(
                  //     alignment: Alignment.bottomRight,
                  //     child: IconButton(
                  //       icon: const Icon(
                  //         Icons.bookmark_border,
                  //       ),
                  //       onPressed: () {},
                  //     ),
                  //   ),
                  // ),
                ],
              ),
            ),
            //descraption and number of comments
            Container(
              width: double.infinity,
              padding: const EdgeInsets.only(
                top: 8,
                left: 25,
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Container(
                  //   width: double.infinity,
                  //   padding: const EdgeInsets.only(
                  //     top: 8,
                  //   ),
                  //   child: RichText(
                  //     text: TextSpan(
                  //       style: const TextStyle(color: primaryColor),
                  //       children: [
                  //         TextSpan(
                  //           text: widget.snap['username'],
                  //           style: const TextStyle(
                  //             fontWeight: FontWeight.bold,
                  //           ),
                  //         ),
                  //         TextSpan(
                  //           text: ' ${widget.snap['description']}',
                  //         ),
                  //       ],
                  //     ),
                  //   ),
                  // ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Leave a Comment