Giter VIP home page Giter VIP logo

Comments (3)

brianegan avatar brianegan commented on June 12, 2024 1

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.

RNMBuser avatar RNMBuser commented on June 12, 2024

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.

brianegan avatar brianegan commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.