Comments (3)
Hey there -- sorry my friend, somehow missed this one!
Overall, I think scoped_model might not be necessary, and InheritedWidget is often enough (this is really just a simple wrapper around the Listenable and InheritedWidget classes).
That said, I think ScopedModel
can work really nicely if you want an imperative API. For example, you can wrap your Model's fields in Getters and Setters like so:
class CounterModel extends Model {
// Private count
int _count = 0;
int get count => _count;
// Every time the count is mutated, update the internal state and notify the listeners!
set count(int newCount) {
_count = newCount;
notifyListeners();
}
}
After providing this small amount of code, you can now update your model imperatively, and the scoped model will take care of notifying any components that are listening!
final counter = CounterModel();
// Print the latest count whenever the model changes
counter.addListener(() { print(counter.count); });
counter.count = 1; // prints 1, because the setter will notify Listeners
Overall, I think this is a really nice / simple pattern if you like the imperative approach and isn't so easily replicated with InheritedWidgets alone.
from scoped_model.
Thanks a lot!
I have another question, please: Suppose I have an AppModel that is composed of 3 classes:
user: (username, token, etc)
cart: (items list)
wishlist: (items list)
Now for some reason I get logged out. I try to call the API from the cart, in order to update it, but get a message such as "access denied". How do I update the user model, and set the token to null? This is quite easy to do in Redux, but I have no idea how this can be done easily in other state-management systems.
from scoped_model.
I think in that case you'd want to treat it the same way you'd treat a Redux action: Set the user as logged out and notify the listeners. You could even even throw an error if you try to make an API call using a null User.
Then, it's a matter of coordinating those calls: Why might you be making an API call to the Cart after the user has logged out and that information is required?
It sounds like there's a race condition: You're first logging out, then performing some API call sometime after. IMO, it's just a matter of clearing that API call when you know the user is logged out and it will produce an error.
from scoped_model.
Related Issues (20)
- Some widget don't refresh after calling notifyListener in flutter 1.22.1
- bloc model value fetch in init function not working
- can't run
- can't run
- Error: Could not find the correct ScopedModel.
- Testing when using scopedModelDescendant HOT 2
- 单独抽离一个方法 如何修改值?
- 不继承 ScopedModelDescendant() 如何修改值?
- void initState() 方法里如何修改值?
- Not Working error
- Target of URI doesn't exist: 'package:scoped_model/scoped_model.dart'.
- Publish null safe stable version
- Error: Could not find the correct ScopedModel. To fix, please: * Provide types to ScopedModel<MyModel> * Provide types to ScopedModelDescendant<MyModel> * Provide types to ScopedModel.of<MyModel>() * Always use package imports. Ex: `import 'package:my_app/my_model.dart'; If none of these solutions work, please file a bug at:
- Exception has occurred. ScopedModelError (Error: Could not find the correct ScopedModel.
- kindly update
- scoped_model issues HOT 6
- Scoped Model - not working after go back to previous page
- Model notifyListeners closes EndDrawer
- ScopedModel
- scoped_model.of() issue 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 scoped_model.