rrousselGit / riverpod

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

example for StreamProvider #33

Closed logemann closed 4 years ago

logemann commented 4 years ago

I would like to see a more detailed example with StreamProvider in the docs. I think in the flutter / firebase context, a really nice example would be the equivalent of this from the provider way of things:

StreamProvider<FirebaseUser>.value(
            value: FirebaseAuth.instance.onAuthStateChanged);

translated to River Pod, it might look like this, but i could be wrong (since i am trying to clean up here, its not 1:1 translation as is):

final firebaseAuthProvider = StreamProvider<FirebaseUser>((ref) {
  ref.onDispose(() {
    // Closes the StreamController when the state of this provider is destroyed.
    FirebaseAuth.instance.signOut();
  });

  return FirebaseAuth.instance.onAuthStateChanged;
});

Please correct me if i am wrong on this.

rrousselGit commented 4 years ago

For firebase you will want to use AutoDisposeStreamProvider, to stop the subscription ASAP when the UI doesn't use the value anymore

logemann commented 4 years ago

good hint. Wasnt aware of the class alltogether ;-)

logemann commented 4 years ago

I think it would be super nice to have little code example inside the API doc like with https://pub.dev/documentation/riverpod/latest/riverpod/StateNotifierProvider-class.html. Would speed up adoption drastically.

And to me its not clear how to handle the types when constructing the provider as well as with the caller of the hook useProvider(firebaseAuthProvider)

AlexHartford commented 4 years ago

I strongly agree. I really love your libraries but it takes me some time to figure out how to use them optimally due to lack of examples. Even something simple would go a long way. That said, I plan to share some of the things I come up with.

rrousselGit commented 4 years ago

I'm finishing #39 first, which will make documenting providers easier. Then I'll document all providers individually.

AlexHartford commented 4 years ago

Tying together the above comments with the newly introduced syntax:

StreamProvider.autoDispose((ref) => FirebaseAuth.instance.onAuthStateChanged)

Thanks for the updates Remi!

logemann commented 4 years ago

@AlexHartford so AutoDisposeStreamProvider is already outdated and one should use StreamProvider.autoDispose() ?

rrousselGit commented 4 years ago

The syntax changed a bit recently, yes. But AutoDisposeStreamProvider and StreamProvider.autoDispose are strictly equivalent.

logemann commented 4 years ago

thanks. Just noticed that i wasnt on latest version.