rrousselGit / riverpod

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

documentation for using riverpod in Dart applications #425

Closed maks closed 2 years ago

maks commented 3 years ago

I love using Riverpod in my Flutter apps. But one of Riverpods advertised features is its independence from Flutter, making it useful for Dart applications.

However apart from mention of using Riverpod in tests, there is not really any documentation for using riverpod outside of Flutter apps.

One example I am working on at the moment: I am building a Dart cli app to work as a "groovebox", essentially an app that sequences music notes, plays back samples, synthesises sounds, etc.

While its currently wrapped in a Flutter app for easier dev ux, it NEEDS to be a plain dart app cli as it will eventually run on a "headless" RPI, taking user input ONLY via a hardware midi controller.

Now I want to use riverpod for this because my pure dart app still has the exactly the same kinds of needs as a Flutter app: user input (via hardware midi controller) and even a UI (via a monochrome 128x64 pixel OLED display).

Currently I have used riverpod for the first bit of app state I need to manage, a "session" which for example provides a beats-per-minute parameter (sorry @rrousselGit switching over to freezed is on my todo list 🙂) that needs to be updated via the UI code and then listened to via the sequencer code.

This is about as close to the "classic counter example" as I think you can get in a real world app.

But due to lack of documentation, I am uncertain if I am doing this correctly. Currently I create the provider and then create a top-level container which I then pass around to each component that needs to usue the provider.

It would be good to document if the above is the right way to use riverpod in a dart app and especially how ProviderContainer should be created, used and if there should be some sort of tree of containers?

Also the way I'm listening to changes doesn't seem right to me so it would be good to have some guidance on the right wya to do this in a non-flutter app.

AlexHartford commented 3 years ago

While I haven't worked with pure-dart riverpod outside of unit testing (as in, take this with a grain of salt), this all looks fine to me, except for passing your ProviderContainer around. I'm not sure that's recommended and/or necessary. If it's working for you, I'd say keep going. If something breaks, that's the first thing I'd look towards. Neat project!

rrousselGit commented 3 years ago

The API for using Riverpod with only dart is not refined yet.

I'm still gathering feedback on the use cases, especially on the "listen to change" part. I'm open to all problems and suggestions.

rrousselGit commented 2 years ago

As a comeback to this, with the new ProviderContainer API, Dart applications should have it better.

You can now directly do:

final provider = Provider((ref) => ...)

void main() {
  final container = ProviderContainer();

  container.listen(
    provider,
    (previous, value) => print('changed'),
  ); 
}
maks commented 2 years ago

Thanks again @rrousselGit ! Given its a bit niche area, I think this issue thread is probably enough docs for anyone wanting to use Riverpod in a Dart (non Flutter) app.

artificerchris commented 2 years ago

I've even been using the new version in an app and it hadn't even occurred to me, but man is this a GitHub ping I am delighted to get. Thank you so much, I can't wait to try this out in pure dart!

Side note: I'm really liking the new ref pattern in widgets and the other stuff going into 1.0, btw, thanks for that too!