River_pod: Add some kind of "transitions" concept on Riverpod

Created on 16 Sep 2020  路  2Comments  路  Source: rrousselGit/river_pod

Hi!
On some inmutable states managements with reactive Event / Action approach they have the possibility for catch all the state changes (in flutter_bloc lib they called Transitions ) only listening on one specific place. On flutter_bloc for example, you can override onTransition function on every bloc you have or in a main listener (BlocObserver) all state changes for every bloc in your app.
This approach is beatiful for Log / Analytics / reproduce errors for example.

I guess I can solve this by integrating flutter_bloc with Riverpod but I prefeer the built-in providers state solutions with StateNotifier or simple State.
Can we make some similar feature without the need of watch all Providers changes?
I think on a new follow modifier that "mark" the Provider and ProviderContainer or other Riverpod coordinator object watch this marked Providers and fire some callback.

Example:

final myFollowedProvider = 
    StateProvider.follow<String>(
        key: "providerLogKey", /* a key for identifie provider */ 
        (ref) => "follow me!"
);

void main() {
  runApp(
    ProviderScope(
      child: MyApp(),
      mainTransitions: (ProviderTransition transition) => {
        /*
          transitions examples      

          Example 1
          {
            "provider": "providerLogKey",
            "currentValue": "",
            "nextValue": "follow me!"
          }

        */
      }
    ),
  );
}

enhancement

Most helpful comment

Are you looking for ProviderObserver?

class Logger extends ProviderObserver {
  @override
  void didUpdateProvider(ProviderBase provider, Object newValue) {
    print('''
{
  "provider": "${provider.name ?? provider.runtimeType}",
  "newValue": "$newValue"
}''');
  }
}

void main() {
  runApp(
    ProviderScope(
      observers: [Logger()],
    ),
  );
}

final counter = StateProvider((ref) => 0, name: 'counter');

All 2 comments

Are you looking for ProviderObserver?

class Logger extends ProviderObserver {
  @override
  void didUpdateProvider(ProviderBase provider, Object newValue) {
    print('''
{
  "provider": "${provider.name ?? provider.runtimeType}",
  "newValue": "$newValue"
}''');
  }
}

void main() {
  runApp(
    ProviderScope(
      observers: [Logger()],
    ),
  );
}

final counter = StateProvider((ref) => 0, name: 'counter');

Amazing! Thanks a lot Remi!

Was this page helpful?
0 / 5 - 0 ratings