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

How to get access to other scoped models in a scoped model file? #75

Open
Feelthewind opened this issue Apr 25, 2019 · 2 comments
Open

Comments

@Feelthewind
Copy link

Feelthewind commented Apr 25, 2019

Hi! I want to get access to _authenticatedUser which is in UserModel from ProductsModel.
What is the most effective way to get it?
I guess i can use ScopedModel.of and pass it as an argument from View. But i don't feel like this is ideal.
Or is it okay to use DI library like get_it in a model file? (UserModel _userModel = locator< UserModel >();)
If it's okay, whether i use singleton or not when registering model using DI library makes any difference?
I guess i have to use singleton to keep states in a model.

class ProductsModel extends Model {
  List<Product> _products = [];

  void addProduct(
      String title, String description, String image, double price) {
    final Product newProduct = Product(
        title: title,
        description: description,
        image: image,
        price: price,
        userEmail: _authenticatedUser.email,
        userId: _authenticatedUser.id);
    _products.add(newProduct);
    notifyListeners();
  }
}

class UserModel extends Model {
  User _authenticaedUser;

  void login(String email, String password) {
    _authenticaedUser = User(id: 'fjekfjk', email: email, password: password);
  }
}
@dakamojo
Copy link

dakamojo commented May 3, 2019

I have played around with different options doing the same thing. Here's what I decided to do, but I'd like feedback on if its appropriate.

I have a global UserModel and a global AppModel. Since my AppModel wants to make changes if my UserModel changes, in the constructor of AppModel I do this.

userModel.addListener(() { <do stuff> });

@fliermanrw
Copy link

fliermanrw commented Jul 23, 2019

Hi! I want to get access to _authenticatedUser which is in UserModel from ProductsModel.
What is the most effective way to get it?
I guess i can use ScopedModel.of and pass it as an argument from View. But i don't feel like this is ideal.
Or is it okay to use DI library like get_it in a model file? (UserModel _userModel = locator< UserModel >();)
If it's okay, whether i use singleton or not when registering model using DI library makes any difference?
I guess i have to use singleton to keep states in a model.

class ProductsModel extends Model {
  List<Product> _products = [];

  void addProduct(
      String title, String description, String image, double price) {
    final Product newProduct = Product(
        title: title,
        description: description,
        image: image,
        price: price,
        userEmail: _authenticatedUser.email,
        userId: _authenticatedUser.id);
    _products.add(newProduct);
    notifyListeners();
  }
}

class UserModel extends Model {
  User _authenticaedUser;

  void login(String email, String password) {
    _authenticaedUser = User(id: 'fjekfjk', email: email, password: password);
  }
}

Do you make use of a single model to pass around (e.g. a MainModel)? The way I use it is the following:

class MainModel extends Model with UserModel, LocationModel, ExampleModel, {}

Like this you can use all variables from these models in one model. Pass the MainModel model everywhere and access a var from the UserModel by e.g. calling widget.model.user.name. To access the model in e.g. your usermodel, pass the mainModel to the function in the userModel:

class ProductModel extends Model {
~~
void exampleFunction(MainModel model, String productName, int y, double z){

products.add(
user: model.user.name,
email: model.user.email,
productname: productName,
);
notifyListeners();
}
}

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

3 participants