Retrieve Amity Posts by Timestamp Observe Multiple Users

Currently I have a function to observe a friend from the method observeFriendPosts. How can I observe multiple users at the same time and have amity posts retrieved in timestamp order and when fetching next page it fetches posts based on the timestamp?


  void observeFriendPosts(String friendId,
      {ScrollController? externalScrollController}) {
    try {
      AppLogger.logInfo(
          "Observing friend posts for friendId: $friendId...");
      scrollController = externalScrollController ?? ScrollController();
      //initialize post live collection
      postLiveCollection = AmitySocialClient.newPostRepository()
          .getPosts()
          .targetUser(friendId)
          .getLiveCollection(pageSize: 20);

      //listen to data changes from live collection
      postLiveCollection.getStreamController().stream.listen((event) {
        AppLogger.logInfo("PostService: observeCommunityPosts: $event");
        // update latest results here
        // setState(() {
        amityPosts.clear();
        amityPosts.addAll(event);
        update();
        // });
      });

      //load first page when initiating widget
      postLiveCollection.loadNext();

      //add pagination listener when srolling to top/bottom
      scrollController!.addListener(paginationListener);
    } catch (exception) {
      AppLogger.logError(
          "Error PostService: observeCommunityPosts: $exception");
    }
      }

Hello, could you please provide more information about your use case? Are you working on implementing a feed that includes posts from friends?

I am trying to create a page where the user can favorite their friends and there is a favorite feed page where the user can see all the feed the user’s favorite friend have posted. (This is not same as community because the user’s favorite friends my not be friends together) Thus I want to observe multiple friends at the same time and have the amity posts retrieved in timestamp order and when fetching next page it fetches posts based on the timestamp. How can I achieve this?

I think you will have to create a private community for each user (for ex. named ‘user friend community’) and after that you will able to get posts in that community.

Observables in a for cycle can make memory leak and many other problems so try to avoid observe too mutch.

1 Like