rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

Need better docs #23

Closed subzero911 closed 4 years ago

subzero911 commented 4 years ago

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 commented 4 years ago

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

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

subzero911 commented 4 years ago

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 commented 4 years ago

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

read = context.read;

subzero911 commented 4 years ago

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 commented 4 years ago

That would be a typical provider setup:

MultiProvider(providers: [
  Provider(create: (_) => LocalStorage()),
  StateNotifierProvider<Counter, int>(create: (_) => Counter()),
]);
rrousselGit commented 4 years ago

I have added this information to the readme