mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.4k stars 310 forks source link

Proper way to create a constructor? #813

Closed DanMossa closed 2 years ago

DanMossa commented 2 years ago

Hello! I have a Store title UserStore. I want this Store to be initialized with UserModel user.

I have this

part 'user_store.g.dart';

class UserStore = UserStoreBase with _$UserStore;

abstract class UserStoreBase with Store {
  late DatabaseService _databaseService;

  @observable
  late UsersModel user;

  UserStoreBase(UsersModel user) {
    _databaseService = getIt.get<DatabaseService>();
    this.user = user;
  }
}

Is this the correct way of doing this? I remember reading something weird happens with constructors and abstract classes.

fzyzcjy commented 2 years ago

part 'user_store.g.dart';

class UserStore = UserStoreBase with _$UserStore;

abstract class UserStoreBase with Store {
  late DatabaseService _databaseService;

  @observable
  UsersModel user;

  UserStoreBase(this.user) {
    _databaseService = getIt.get<DatabaseService>();
  }
}
DanMossa commented 2 years ago

@fzyzcjy Right, I don't want it to be late, I want it to be final like a normal constructor