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

Migrate from provider to context_plus #12

Open
passsy opened this issue Jul 9, 2024 · 6 comments
Open

Migrate from provider to context_plus #12

passsy opened this issue Jul 9, 2024 · 6 comments
Labels
good first issue Good for newcomers question Further information is requested

Comments

@passsy
Copy link
Contributor

passsy commented Jul 9, 2024

I'd like to have a quick small guide how to migrate from provider to context_plus.

Basic usage works, but I'd like to know if I can migrate more advanced topics like ProxyProvider or sub scopes.

@s0nerik
Copy link
Owner

s0nerik commented Jul 10, 2024

I've almost finished the site with various usage examples. Hopefully, I'll publish it towards the end of the next week. Will let you know.

Regarding the migration, there won't be 1:1 correspondence, but I believe context_plus supports the same amount of usecases as Provider, or maybe even more.

You can:

  • Create value eagerly and have it auto-disposed via .bind() (~Provider(lazy: false)
  • Create value lazily and have it auto-disposed via .bindLazy() (~Provider(lazy: true)
  • Provide an already created value via .bindValue() (~ValueProvider)
  • Provide a value based on other values via .bind(key: ), .bindLazy(key: ) (~ProxyProvider). Example:
    final value1 = value1Listenable.watch(context);
    final value2 = value2Listenable.watch(context);
    final value3 = _value3Ref.bind(
      context,
      () => Value3(value1, value2),
      key: (value1, value2),
    );
    Now, whenever value1 or value2 changes, the Value3 will get recreated. Works more-or-less like keys on widgets.
  • Track asynchronous operations state outside the widget tree with AsyncListenable and provide it either via Ref<AsyncListenable>.bind() or as a part of some other object.

Also, Ref<AnimationController> and a couple of other types of refs provide an additional vsync parameter so that you can create and bind them easily as well.

The whole API is intentionally kept minimal-ish:

Ref.bind() - create eagerly, then bind
Ref.bindLazy() - bind, then create lazily upon first request
Ref.bindValue() - just bind
(Stream/Future/Listenable/ValueListenable/AsyncListenable/Ref<Stream>/Ref<Future>/Ref<Listenable>/Ref<ValueListenable>/Ref<AsyncListenable>/...).watch() - rebuild on any notification
(Stream/Future/Listenable/ValueListenable/AsyncListenable/Ref<Stream>/Ref<Future>/Ref<Listenable>/Ref<ValueListenable>/Ref<AsyncListenable>/...).watchOnly() - rebuild only when selected value changes upon notification

Hope that helps. Stay tuned for the site, I love how it pans out so far.

@s0nerik s0nerik added good first issue Good for newcomers question Further information is requested labels Jul 31, 2024
@yousefak007

This comment was marked as off-topic.

@s0nerik

This comment was marked as off-topic.

@yousefak007

This comment was marked as off-topic.

@yousefak007

This comment was marked as off-topic.

@s0nerik
Copy link
Owner

s0nerik commented Aug 28, 2024

@yousefak007 I've created a separate issue to track the potential Ref.bindProxy() implementation. Feel free to comment there: #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants