Flutter: Unable to fetch posts for any community

After login , user is not able to fetch posts available for any community. Please find the code below:

_controller = PagingController(
pageFuture: (token) {
return AmitySocialClient.newPostRepository()
.getPosts()
.targetCommunity(communityId)
// supported data types are IMAGE, VIDEO, and FILE
.types(
[AmityDataType.IMAGE, AmityDataType.VIDEO, AmityDataType.TEXT])
//feedType could be AmityFeedType.PUBLISHED, AmityFeedType.REVIEWING, AmityFeedType.DECLINED
.feedType(AmityFeedType.REVIEWING)
.sortBy(AmityPostSortOption.LAST_CREATED)
.includeDeleted(false)
.getPagingData(token: token, limit: 20);
},
pageSize: 20,
);

_controller.addListener(
  () {
    if (_controller.error == null) {
      amityPosts.clear();
      amityPosts.addAll(_controller.loadedItems);
      notifyListeners();
    } else {
    }
  },
);

Please help.

Here, listener is not getting called at all.

Good morning @maddy0028!

May I understand your use-case of the screen please?
It seems like you’re trying to query reviewing posts in a particular community. If you’d like to see all of the published posts in a community, you may consider using feedType(AmityFeedType. PUBLISHED) instead.

Thank you!

Good morning Trust.

I have already checked feedType as AmityFeedType.PUBLISHED, but same result. Not getting anything. Then I changed this to AmityFeedType.REVIEWING. Still the same.

Am I missing some concept of Amity?

Thank you @maddy0028!

Let me check this with the team, will keep you updated.

Thank you @Trust for your response.
I have one more doubt related to this. I am using below code to login user to Amity cloud:

AmityCoreClient.login(userId).displayName(name).submit();

Not using token to while user login. Is it the reason I am unable to fetch posts?

Hi @Trust Good morning. Any update on above issue?

Good Morning @maddy0028,

Thank you for reaching out to us, and please apologize for my delayed response.

Based on your query, there could be several reasons why the user is not able to fetch posts available for a particular community. Let’s go through them step by step:

  1. SDK Initialization and Authentication: Make sure that the SDK is properly initialized and set up with an API key at the application level. You can refer to this guide for assistance. Further, ensure that the login procedure is completed before initiating any other actions in your application. For more information on this, you can check out this resource.
    Regarding the authentication token, we strongly recommend using authToken for authentication in a production environment. The option to proceed without an authToken should only be utilized during the development stage.

  2. Invalid Post Types: In the types query option, you might not need to specify if you wish to query all the posts in a community. It’s possible that the existing posts in a community do not match the specified query option.

  3. Posts Availability: It’s possible that the targeted community doesn’t have any posts yet. Please confirm if there is at least one post created in the community you are trying to access. To learn more about creating posts, please visit this link.

Please check these potential issues and see if the situation improves. If you are still facing difficulties, don’t hesitate to let us know. We’re here to help!

Best Regards,
Trust.

Hi @Trust, thank you for your response. I have checked your steps, below are the findings:

  1. SDK Initialization and Authentication: I have set up sdk before making any call to fetch posts.
    Currently for dev I am not using auth token, will implement that in produciton.
  2. I have removed post type from query. Still not getting posts. (I am able to post any content to
    community which I can see on Amity protal).
  3. There are couple of posts available for the community i am trying to fetch.

Am I missing any code to fetch the post.

PFA for post available in my community.

May I see the full code snippets including corresponding widgets please?
Thank you @maddy0028!

Sure @Trust.

In main.dart, I have added below code:
///Set up Amity social cloud
await AmityCoreClient.setup(
option: AmityCoreClientOption(
apiKey: amityApiKey,
httpEndpoint: AmityRegionalHttpEndpoint.SG,
showLogs: true,
),
sycInitialization: true,
);

Then I am checking if user is logged in or not using AmityCoreClient.isUserLoggedIn(). Unfortunately user is logged out every time I restart the app. So. I am logging user again using this code:
AmityCoreClient.login(userId).displayName(name).submit();

I have created a provider to fetch and create posts:

Inside my provider, to fetch posts:
void initPagingController(String communityId) {
_controller = PagingController(
pageFuture: (token) {
return AmitySocialClient.newPostRepository()
.getPosts()
.targetCommunity(communityId)
// supported data types are IMAGE, VIDEO, and FILE
// .types(
// [AmityDataType.IMAGE, AmityDataType.VIDEO, AmityDataType.TEXT])
//feedType could be AmityFeedType.PUBLISHED, AmityFeedType.REVIEWING, AmityFeedType.DECLINED
// .feedType(AmityFeedType.PUBLISHED)
// .sortBy(AmityPostSortOption.LAST_CREATED)
.includeDeleted(false)
.getPagingData(token: token, limit: 20);
},
pageSize: 20,
);

_controller.addListener(
  () {
    if (_controller.error == null) {
      amityPosts.clear();
      amityPosts.addAll(_controller.loadedItems);
      notifyListeners();
    } else {
      _controller.retry();
    }
  },
);

}

Calling initPagingController method from initState() method of showPosts screen and listening to amityPosts list changes using Selector.

Added print statement in pageFuture and _controller.addListener(). Which is not getting printed.

One more thing. I am able to post any content from my flutter app, which i can confirm from Amity portal.

Thanks

Hi @Trust, Good morning, I am able to fetch the posts available for any community. I was missing this in code while attaching listener to _controller:

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_controller.fetchNextPage();
});

I will raise another issue if I get any issue while working on Amity Social Cloud.

Thanks