12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:veloe_kemono_party_flutter/kemono_client.dart';
- import 'package:veloe_kemono_party_flutter/models/post.dart';
- class PostsNotifier extends StateNotifier<AsyncValue<List<Post>>> {
- final String creatorId;
- final String service;
- final KemonoClient client;
- int _currentPage = 0;
- PostsNotifier({
- required this.creatorId,
- required this.service,
- required this.client,
- }) : super(const AsyncValue.loading()) {
- loadInitial();
- }
- Future<void> loadInitial() async {
- state = const AsyncValue.loading();
- try {
- final posts = await client.getPosts(creatorId, service, 0);
- state = AsyncValue.data(posts);
- _currentPage = 1;
- } catch (e) {
- state = AsyncValue.error(e, StackTrace.current);
- }
- }
- Future<void> loadNext() async {
- if (state.isLoading) return;
-
- final previousPosts = state.valueOrNull ?? [];
- // Add explicit type parameter
- state = AsyncValue<List<Post>>.loading().copyWithPrevious(state);
-
- try {
- final newPosts = await client.getPosts(
- creatorId,
- service,
- _currentPage * 50
- );
- print('Loading page $_currentPage with offset ${_currentPage * 20}');
- state = AsyncValue.data([...previousPosts, ...newPosts]);
- _currentPage++;
- } catch (e) {
- // Specify type for error state
- state = AsyncValue<List<Post>>.error(e, StackTrace.current)
- .copyWithPrevious(AsyncValue.data(previousPosts));
- }
- }
- }
|