getFrontPagePosts method Null safety
- PostDataModel postsDataNotifier,
- bool getWholeNew
Static Method Fetching the Reddit front page posts
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 getFrontPagePosts(PostDataModel postsDataNotifier, bool getWholeNew) async {
List<SubredditRef> fetchedForIcon = [];
ApiLauncher redditApi = ApiLauncher();
Submission fetchedSubmission;
List<Post> posts = [];
var chosenMethod;
String? after;
Post newPost;
if (redditApi.isFlowCreated() == false)
await redditApi.createRedditFlow();
if (getWholeNew == false)
after = postsDataNotifier.getLastFetchedItem();
switch (postsDataNotifier.fetchedCategory) {
case "new":
chosenMethod = redditApi.redditApi.front.newest(
limit: 25,
after: after
);
break;
case "hot":
chosenMethod = redditApi.redditApi.front.hot(
limit: 25,
after: after
);
break;
case "top":
chosenMethod = redditApi.redditApi.front.top(
limit: 25,
after: after
);
break;
case "rising":
chosenMethod = redditApi.redditApi.front.rising(
limit: 25,
after: after
);
break;
case "best":
chosenMethod = redditApi.redditApi.front.best(
limit: 25,
after: after
);
break;
case "controversial":
chosenMethod = redditApi.redditApi.front.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) {
Subreddit tmp = (await fetchedForIcon[0].populate());
newPost.subIcon = tmp.iconImage.toString().replaceAll("amp;", "");
}
posts.add(newPost);
}
if (getWholeNew == false)
postsDataNotifier.addPostListToList(posts);
else
postsDataNotifier.setPostList(posts);
}