rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
5.82k stars 888 forks source link

Support ProviderSubscription read inside useEffect dispose. #3491

Open a1573595 opened 1 month ago

a1573595 commented 1 month ago

Is your feature request related to a problem? Please describe. Reference #3486. I want to lazily load data when entering the page, cache them after the data is loaded, and release them when leaving the page.

Describe the solution you'd like As shown below, support sub.read processing inside useEffect, which will currently throw an error(Bad state: called ProviderSubscription.read on a subscription that was closed).

useEffect(() {
      final subscription = ref.listenManual(Provider, (prev, next) {});

      return () {
        ref.read(Provider.notifier).update((state) => !state);
        subscription.close();
      };
}, const []);