No post from Flutter SDK Feed & Timeline

Hello,

I’m using Flutter SDK to query posts from UserFeed, CommunityFeed, and Global Feed. Despite there being posts on Amity Social Cloud Console, when I query either three of them, it didn’t return any posts (I checked UserID, and CommunityID that has posts).

This is code from Amity Docs:

void queryUserFeed(String userId) {
    //initiate the PagingController
    _controller = PagingController(
      pageFuture: (token) => AmitySocialClient.newFeedRepository()
          .getUserFeed(userId)
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(
        () {
          if (_controller.error == null) {
            //handle results, we suggest to clear the previous items
            //and add with the latest _controller.loadedItems
            posts.clear();
            posts.addAll(_controller.loadedItems);
            //update widgets
          } else {
            //error on pagination controller
            //update widgets
          }
        },
      );

    if (kDebugMode) {
      print(posts.length);
    }
  }

This is output in my terminal:

I/flutter (25357): 0

Please support me.

1 Like

Hello @khoilr,
It seems that the print function was execute before PagingController finish fetching.
Could you try the following code instead?

void queryUserFeed(String userId) {
    //initiate the PagingController
    _controller = PagingController(
      pageFuture: (token) => AmitySocialClient.newFeedRepository()
          .getUserFeed(userId)
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(
        () {
          if (_controller.error == null) {
            //handle results, we suggest to clear the previous items
            //and add with the latest _controller.loadedItems
            posts.clear();
            posts.addAll(_controller.loadedItems);
            if (kDebugMode) {
            print(posts.length);
            }
            //update widgets
          } else {
            //error on pagination controller
            //update widgets
          }
        },
      );

  }

Same issue for us, 0 posts in the controller. Printing has nothing to do with it. The controller itself has 0 loadedItems. Is that a timing issue? When and where to call the initPagingController?

It would be super helpful if you could put the queryUserFeed function in a broader context of an example class. Your Flutter documentation on feeds is quite limited, unfortunately.

PS: Awaiting the newFeedRepository actually returns a list of the posts. Instead of coding our own solution around this we would love to follow the officially intended way.

  queryUserFeed(String userId) async {
    var test = await AmitySocialClient.newFeedRepository()
        .getUserFeed(userId)
        .getPagingData(limit: 20);

    return test;
  }

Hello, based on your question, await is mandatory for this as the method will call to our system which is an asynchronous call so you will have to wait until the method successfully returns the data in order to render it on the front end side. In addition you can use this code snippet as well.

AmitySocialClient.newFeedRepository()
.getUserFeed(userId)
.getPagingData(limit: 20)
.then((value) {});

Thanks for getting back to me. Adding async await and/or .then((value) {}) to the newFeedRepository call from the documentation has no effect. Still 0 loadedItems in the _controller.

Could you please try the code below:

AmitySocialClient.newFeedRepository()
        .getUserFeed(userId)
        .getPagingData(limit: 20)
        .then((value) {
      print("check amity post list ${value}");
    });

Thanks to the awesome help of Mark and the Amity engineer team the issue is solved. The pagingController is empty at first call.

_controller.fetchNextPage();

is required to pull the first results. Here is the updated initPagingController function that works for us.

  void initPagingController(String userId) async {
    //inititate the PagingController
    _controller = PagingController(
      pageFuture: (token) => AmitySocialClient.newFeedRepository()
          .getUserFeed(userId)
          .includeDeleted(false)
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(
        () {
          if (_controller.error == null) {
            //handle results, we suggest to clear the previous items
            //and add with the latest _controller.loadedItems
            setState(() {
              amityPosts.clear();
              amityPosts.addAll(_controller.loadedItems);
            });
            // update widgets
          } else {
            //error on pagination controller
            //update widgets
          }
        },
      );
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _controller.fetchNextPage();
    });
  }