getAllPosts method
Get all posts
Implementation
Future<List<Post>> getAllPosts() async {
List<Post> posts;
final initialJson = await fetchResponse();
checkPrivate(instaApiFromJson(initialJson).graphql!.user!.isPrivate!);
final id = instaApiFromJson(initialJson).graphql!.user!.id;
bool hasNextPage = instaApiFromJson(initialJson)
.graphql!
.user!
.edgeOwnerToTimelineMedia!
.pageInfo!
.hasNextPage!;
String? endCursor = instaApiFromJson(initialJson)
.graphql!
.user!
.edgeOwnerToTimelineMedia!
.pageInfo!
.endCursor;
final initialEdges = instaApiFromJson(initialJson)
.graphql!
.user!
.edgeOwnerToTimelineMedia!
.edges!;
posts = getNestedPosts(initialEdges);
final totalPosts = instaApiFromJson(initialJson)
.graphql!
.user!
.edgeOwnerToTimelineMedia!
.count!;
int counter = 12;
if (totalPosts > postsLimit)
log('Total Posts are more than the specified limit, this might result in a huge time to load posts and might surpass Instagram\'s no. of requests/hour limit',
name: "Insta Public API");
while (hasNextPage) {
if (counter > postsLimit) {
log('loaded $counter posts!', name: "Insta Public API");
break;
}
final json = await fetchNextResponse(
id, endCursor!.substring(0, endCursor.length - 2));
hasNextPage = nextResponseFromJson(json)
.data!
.user!
.edgeOwnerToTimelineMedia!
.pageInfo!
.hasNextPage!;
endCursor = nextResponseFromJson(json)
.data!
.user!
.edgeOwnerToTimelineMedia!
.pageInfo!
.endCursor;
final edges = nextResponseFromJson(json)
.data!
.user!
.edgeOwnerToTimelineMedia!
.edges!;
final tempPosts = getNestedPosts(edges);
posts += tempPosts;
counter += tempPosts.length;
}
return posts;
}