Skip to content

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

Closed
Sineaggi opened this issue Aug 20, 2021 · 6 comments
Closed

Best practice: Unit test sample in README does not compile #223

Sineaggi opened this issue Aug 20, 2021 · 6 comments

Comments

@Sineaggi
Copy link

This code no longer compiles

class AppModel {}
class DbService {}

class UserManager {
  AppModel appModel;
  DbService dbService;

  UserManager({AppModel appModel, DbService dbService}) {
    this.appModel = appModel ?? getIt.get<AppModel>();
    this.dbService = dbService ?? getIt.get<DbService>();
  }
}

It complains with

Non-nullable instance field 'appModel' must be initialized.
Non-nullable instance field 'dbService' must be initialized.

Is it recommended to write every setup like this now, or is there another recommend way?

class UserManager {
  AppModel appModel;
  DbService dbService;

  UserManager({required this.appModel, required this.dbService});
}
getIt.registerFactory<AppModel>(() => AppModel());
getIt.registerFactory<DbService>(() => DbService());
getIt.registerFactory(() => UserManager(appModel: getIt<AppModel>(), dbService: getIt<DbService>()));
@Sineaggi
Copy link
Author

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
}

@Sineaggi Sineaggi changed the title Unit test sample in README does not compile Best practice: Unit test sample in README does not compile Aug 22, 2021
@Sineaggi
Copy link
Author

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
}

@escamoteur
Copy link
Collaborator

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

@escamoteur
Copy link
Collaborator

does that help?

@Sineaggi
Copy link
Author

Sineaggi commented Feb 6, 2022

Sorry, what do you mean by

After null safety that parameters should be either "required" but that's not what you wan't.

Is the use of required a problem?

@escamoteur
Copy link
Collaborator

when you add required you have always to manually inject the dependencies so it wouldn't make any sense to additionally request them from get_it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants