-
-
Notifications
You must be signed in to change notification settings - Fork 153
Best practice: Unit test sample in README does not compile #223
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
Comments
Also, are there any general guidelines for how injected services should be named? Dart makes fields public by default, which means someone could feasibly call functions on one of those injected fields. class AppModel{}
class DbService{}
class UserManager {
AppModel appModel;
DbService dbService;
UserManager(this.appModel, this.dbService);
}
void test() {
final userManager = UserManager(AppModel(), DbService());
userManager.appModel.someFunction(); // the function that shouldn't be exposed from the UserManager
} |
Maybe something like this? class AppModel{}
class DbService{}
class UserManager {
final AppModel _appModel;
final DbService _dbService;
UserManager._(this._appModel, this._dbService);
factory UserManager({required AppModel appModel, required DbService dbService}) => UserManager._(appModel, dbService);
}
void test() {
final userManager = UserManager(appModel: AppModel(), dbService: DbService());
userManager.appModel.someFunction(); // no longer compiles
} |
Hi, sorry for not looking into this earlier, but I had some mental health problems the last half year. Sorry, you are right. After null safety that parameters should be either "required" but that's not what you wan't. you have to declare them nullable class UserManager {
AppModel appModel;
DbService dbService;
UserManager({AppModel? appModel, DbService? dbService}) {
this.appModel = appModel ?? getIt.get<AppModel>();
this.dbService = dbService ?? getIt.get<DbService>();
}
} |
does that help? |
Sorry, what do you mean by
Is the use of |
when you add |
This code no longer compiles
It complains with
Is it recommended to write every setup like this now, or is there another recommend way?
The text was updated successfully, but these errors were encountered: