Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentation for using riverpod in Dart applications #425

Closed
maks opened this issue Apr 7, 2021 · 5 comments
Closed

documentation for using riverpod in Dart applications #425

maks opened this issue Apr 7, 2021 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@maks
Copy link

maks commented Apr 7, 2021

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.

@maks maks added enhancement New feature or request needs triage labels Apr 7, 2021
@AlexHartford
Copy link
Contributor

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
Copy link
Owner

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 rrousselGit added documentation Improvements or additions to documentation and removed enhancement New feature or request labels Oct 2, 2021
@rrousselGit
Copy link
Owner

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
Copy link
Author

maks commented Oct 27, 2021

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.

@maks maks closed this as completed Oct 27, 2021
@artificerchris
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants