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

Need better docs #23

Closed
subzero911 opened this issue Jun 15, 2020 · 6 comments
Closed

Need better docs #23

subzero911 opened this issue Jun 15, 2020 · 6 comments

Comments

@subzero911
Copy link

subzero911 commented Jun 15, 2020

As far as I can see, State Notifier is a great and underestimated package that solves two separate problems:

  1. Setup Store ((that's how I call a business-logic class provided by Provider) to be state-based (as in Redux or BLoC), that allows me to move all the data to states and access the current state from UI.
    Thereby, I can use all the benefits of immutable states (for example, creating an undo/redo with state history), but without complexity of patterns mentioned above.

  2. Locate services.
    Say, if I have a custom configured Dio-HttpClient, before I should have made it a singleton in order to access it from Store. Because I do not have a context there.
    Now I can use read / update methods to directly get this Dio client instance by its Type.

Needs more explanation.
Seems like there's no Selector or context.select() for StateNotifier? What if I'm getting the same state with small data changes, and I want to subscribe only to those changes selectively, not the whole state updates?
Can I have a mixed Store that both extends StateNotifier and with ChangeNotifier? So it has one part of its data in states, and another part inside the store? (sorry, if it's a silly question)

@rrousselGit
Copy link
Owner

Seems like there's no Selector or context.select() for StateNotifier?

Provider's official Selector/context.select works for StateNotifier.

@subzero911
Copy link
Author

subzero911 commented Jun 15, 2020

How exactly read/update works? Is it something like get_it?
Do I need to create a DBService/HTTPService/...OtherService instance beforehand (in main() function)? Do I need to make it a singleton?

@rrousselGit
Copy link
Owner

It uses context.read internally. It just abstracts the BuildContext so that you don't have to care

read = context.read;

@subzero911
Copy link
Author

The example from documentation is not clear:

class Counter extends StateNotifier<int> {
  Counter(): super(0)

  void increment() {
    state++;
    read<LocalStorage>().writeInt('count', state);
  }
}

What is LocalStorage, and when had you created it?
How can I read it from a business-logic class? I don't have a context there.

@rrousselGit
Copy link
Owner

That would be a typical provider setup:

MultiProvider(providers: [
  Provider(create: (_) => LocalStorage()),
  StateNotifierProvider<Counter, int>(create: (_) => Counter()),
]);

@rrousselGit
Copy link
Owner

I have added this information to the readme

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

No branches or pull requests

2 participants