Comments (15)
Fair.
It's becoming O(N) for dispatching and O(1) for adding/removing listeners — which is better
from riverpod.
I'm promoting the usage of StateNotifier instead.
from riverpod.
Simply make a class that holds both of your variables
class State {
final int count;
final bool isLoggedIn;
}
class MyNotifier extends StateNotifier<State> {
...
}
from riverpod.
That makes me wonder if you are still using Flutter's ChangeNotifier when extending a model class or if Riverpod provides its own one that you should import instead.
from riverpod.
Yeah, I was in fact reading the following article.
As I see it, Freezed is a perfect helper for StateNotifier, as you can now create proper events for the UI using unions. However, I haven't find myself more articles / tutorials about StateNotifier and its uses / advantages.
The problem I see for the migration is that StateNotifier logic differs from ChangeNotifier, as instead of declaring "complex" classes and handling the state there, you are now splitting the information into several pieces, as it based on the ValueNotifier idea.
Just as an example, I was taking a look at the StateProvider doxumentation code. If using a ChangeNotifier class, I would have declared both "selectedProductIdP" and "Products" directly there. However, as it now uses StateNotifier, it is split in two different providers now. I guess you could still create a class having these two elements and then override the equals method or use the equatable library, but I don't think that would be a good solution. What I mean is that it was not trivial for me how to handle these situations at first.
Furthermore, if you also include Freezed to the mix, using inmutable data is also a game-changer in how you think about the data and how you organize your code. I would really love to have more examples showing how to change your way of thinking when developing applications with these new solutions.
from riverpod.
The examples in the example folder all uses StateNotifier.
Is there anything missing that you would like to be added?
from riverpod.
I will take a deep look to them and open a new issue if I miss anything. Thanks!
from riverpod.
I'm not clear how to use StateNotifier is supposed to work with the common use case of having a composite model and the filtering on specific properties. Is there an example that shows that?
from riverpod.
@esDotDev The to-do example uses StateNotifer
from riverpod.
Yes but it still seems to only support dispatching one type of payload. My models look more like this:
class CounterModel extends ChangeNotifier {
int get count1 => _count1;
int _count1;
set count1(int count1) {
_count1 = count1;
notifyListeners();
}
bool get isLoggedIn => _isLoggedIn;
bool _isLoggedIn;
set isLoggedIn(bool isLoggedIn) {
_isLoggedIn = isLoggedIn;
notifyListeners();
}
}
I know I could do it with ChangeNotifierProvider, trying to understand the State equivalent.
from riverpod.
Oh my... that is pretty :D
from riverpod.
For some reason StateNotifier class does not exist, so I don't know how to implement the code snippet shown here:
Just a comment on the docs in general is I find they just skip over the basics of how to use it, it presents an extremely simple use case (basic change notifier), and then jumps right into advanced use cases that start mixing hooks.
Would be nice to slow down a bit and just explain, here's how you watch, here's how you select, here's how you read. Here's a few flavors of each. Then go onto more complex stuff?
from riverpod.
You need to import package:state_notifier
I may re-export it in Riverpod at some point. I'm not sure
Do you mind making a separate issue about the issues you found with the documentation?
I wrote both the package and the doc, so it's hard for me to know what people do not understand
from riverpod.
Sure thing, will do, thanks for the quick response as always :)
from riverpod.
from riverpod.
Related Issues (20)
- Is it possible to do eager-initialisation of ProviderScopes? HOT 1
- examples/pub dependencies and auth problems HOT 1
- The subclass of AutoDisposeNotifier can always obtain internationalization, even if the phone's system Settings are Chinese, and finally English HOT 2
- Option to ignore previous state in AsyncValue by default HOT 3
- Support async `onDispose` HOT 3
- invalidation forwarding HOT 5
- docs: invalid use of `Uri.https` in readme HOT 1
- Read a HOT 1
- Read a "disposed" notifier HOT 1
- Refactoring Propositions dropping out when updating flutter HOT 2
- Add exception handling to 'ref.refresh()' HOT 6
- Add something like `ref.watchStream(...)` HOT 10
- ProviderScope state wrongly disposed possible bug HOT 3
- Add Japanese docs
- [riverpod_generator] `dependencies` does not respect `provider_family_name_suffix` option
- StateNotifierProvider always refreshs when invalidated problem still exists HOT 3
- Error: 'LintCode' is imported from both 'package /src/dart/error/lint_codes.dart' and 'package /src/lint_codes.dart' on Flutter 3.24.0 with build_runner HOT 3
- VS Code refactoring options are not available with Flutter 3.24 stable release HOT 4
- `initialValue`-like behaviour for the FutureProvider HOT 7
- `flutter` dependency included when using Riverpod in a server-side environment HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from riverpod.