Context
I have the following RiverPagedBuilder:
@override
Widget build(BuildContext context, WidgetRef ref) =>
RiverPagedBuilder<Page, Item>(
firstPageKey: Page(
key: pageKey,
orderBy: 'publishedAt',
descending: true,
),
limit: 50,
provider: paginatedControllerProvider,
itemBuilder: (context, item, index) => ItemListTile(
item,
),
pagedBuilder: (controller, builder) => PagedSliverList.separated(
pagingController: controller,
builderDelegate: builder,
separatorBuilder: (ctx, i) => const Divider(
thickness: 0.8,
height: 2,
),
),
);
And here is the paginatedControllerProvider:
part 'paginated_controller.freezed.dart';
class PaginatedController extends PagedNotifier<Page, Item> {
final ItemRepository _repository;
final Logger logger;
PaginatedController(this._repository, this.logger)
: super(
load: (page, limit) {
logger.i('Fetching next $limit of $page');
return _repository
.listItems(
itemId: page.itemId,
orderBy: page.orderBy,
descending: page.descending,
lastItem: page.lastItem,
limit: limit,
)
.first;
},
nextPageKeyBuilder: (List<Item>? lastItems, Page page, int limit) =>
(lastItems == null || lastItems.length < limit)
? null
: Page(
itemId: page.itemId,
orderBy: page.orderBy,
descending: page.descending,
lastItem: lastItems.last.publishedAt,
),
);
}
final paginatedControllerProvider = StateNotifierProvider<
PaginatedController,
PagedState<Page, Item>>(
(ref) => PaginatedController(
ref.read(itemRepositoryProvider),
ref.read(loggerProvider),
),
);
@freezed
class Page with _$Page {
const Page._();
const factory Page({
required String itemId,
required String orderBy,
required bool descending,
@Default(null) dynamic lastItem,
}) = _Page;
}
Problem
This works great as long as the itemId does not change. When I navigate to a page the first time, it pulls in the expected lastest items (descending order by publishedAt). And then after that, on scrolling it loads the older items, again as expected.
However, when I navigate to a different item page (i.e. itemId is different), it adds the records for this new item to the existing state of records.
I would like to clear the state when the itemId changes. How can I achieve this (or am I not fundamentally using it in a different way)?
Context
I have the following
RiverPagedBuilder:And here is the
paginatedControllerProvider:Problem
This works great as long as the
itemIddoes not change. When I navigate to a page the first time, it pulls in the expected lastest items (descending order by publishedAt). And then after that, on scrolling it loads the older items, again as expected.However, when I navigate to a different item page (i.e.
itemIdis different), it adds the records for this new item to the existing state of records.I would like to clear the state when the
itemIdchanges. How can I achieve this (or am I not fundamentally using it in a different way)?