ftognetto / riverpod_infinite_scroll

A plugin for Flutter's infinite_scroll_pagination that works with Riverpod.
MIT License
27 stars 23 forks source link

Filtering using another provider #43

Closed alguintu closed 1 year ago

alguintu commented 1 year ago

I've been trying to implement the suggested filtering method in Issue#31 for a couple of days now, but running into problems.

class HistoryStateNotifier extends PagedNotifier<int, Transaction> {

  HistoryStateNotifier({required this.ref, this.selectedFilter = 'all'})
      : super(
          load: (page, limit) => ref
              .watch(apiProvider)
              .getTransactions(page, limit, selectedFilter)
              .then((value) => value.transactions),
          nextPageKeyBuilder: NextPageKeyBuilderDefault.mysqlPagination,
        );

}

final selectedHistoryFilter = StateProvider<String>((_) => 'all');

final historyProvider =
    StateNotifierProvider<HistoryStateNotifier, PagedState<int, Transaction>>(
  (ref) { 
    final selectedFilter = ref.watch(selectedHistoryFilter);
    return HistoryStateNotifier(
      ref: ref,
      selectedFilter: selectedFilter
    );
  });
// And then change the selectedHistoryFilter in the ui as you wish!

So I tried to implement this in a widget that passes a new string to the filter in the useEffect:

class SomeWidget extends HookConsumerWidget {
  final String filter;

 const SomeWidget({required this.filter, Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    useEffect(() {
      Future.microtask(() => ref.read(selectedHistoryFilter.notifier).state = filter);
      return () {};
    }, []);

    return Scaffold(
     body: RiverPagedBuilder<int, Transaction>(
         provider: historyProvider,
         // assume everything else here is set
     ),
    );
}   

When I do this approach, it loads correctly on the first call to SomeWidget(filter: someFilter), but when the view is popped, and then SomeWidget(filter: someOtherFilter) is called again, it says that historyProvider has been disposed before (I assume from when the first activity was popped), and I don't know how to fix it, or how to use the providers properly from here.

I've been reading up on providers and everything I could find regarding this, but still stuck at the moment. Any link / example / hint would be greatly appreciated. Thank you.

alguintu commented 1 year ago

Duplicated due to bad connectivity. Sorry.