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>> { 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 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 loadNext() async { if (state.isLoading) return; final previousPosts = state.valueOrNull ?? []; // Add explicit type parameter state = AsyncValue>.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>.error(e, StackTrace.current) .copyWithPrevious(AsyncValue.data(previousPosts)); } } }