brianegan/scoped_model

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

Feelthewind opened this issue · 2 comments

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);
  }
}

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> });

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();
}
}