Skip to content

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

@Feelthewind

Description

@Feelthewind

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions