getSubredditPosts method Null safety
- SubRedditModel subRedditDatas,
- bool getWholeNew
Static Method Fetch posts from a Subreddit
Works the same way as getFrontPagePosts static method
Cheks if the installed flow is created. It creates it if the installed flow
isn't created and fetch the posts according to the filter. Store the
posts datas using the PostDataModel Provider.
If getWholeNew
boolean is true, it wipes the post data list and re-fill it.
If getWholeNew
boolean is false, it adds at the end of the list.
Implementation
static getSubredditPosts(SubRedditModel subRedditDatas, bool getWholeNew) async {
List<SubredditRef> fetchedForIcon = [];
ApiLauncher redditApi = ApiLauncher();
Submission fetchedSubmission;
List<Post> posts = [];
var chosenMethod;
Subreddit tmp;
String? after;
Post newPost;
if (redditApi.isFlowCreated() == false)
await redditApi.createRedditFlow();
if (getWholeNew == false)
after = subRedditDatas.getLastFetchedItem();
switch (subRedditDatas.fetchedCategory) {
case "new":
chosenMethod = subRedditDatas.subredditInfo.rawData!.newest(
limit: 25,
after: after
);
break;
case "hot":
chosenMethod = subRedditDatas.subredditInfo.rawData!.hot(
limit: 25,
after: after
);
break;
case "top":
chosenMethod = subRedditDatas.subredditInfo.rawData!.top(
limit: 25,
after: after
);
break;
case "rising":
chosenMethod = subRedditDatas.subredditInfo.rawData!.rising(
limit: 25,
after: after
);
break;
case "controversial":
chosenMethod = subRedditDatas.subredditInfo.rawData!.controversial(
limit: 25,
after: after
);
break;
}
await for (var frontPost in chosenMethod) {
fetchedSubmission = frontPost as Submission;
newPost = Post.fromMap(fetchedSubmission);
fetchedForIcon = await redditApi.redditApi.subreddits.searchByName(newPost.subReddit, exact: true);
if (fetchedForIcon.length > 0) {
tmp = (await fetchedForIcon[0].populate());
newPost.subIcon = tmp.iconImage.toString().replaceAll("amp;", "");
}
posts.add(newPost);
}
if (getWholeNew == false)
subRedditDatas.addPostListToList(posts);
else
subRedditDatas.setPostList(posts);
}