get_it icon indicating copy to clipboard operation
get_it copied to clipboard

Why missing required factory parameter is silently replaced with null?

Open AlexanderFarkas opened this issue 4 years ago • 6 comments

class SettingsBloc {
  final Event event;

  SettingsBloc(@factoryParam this.event);
}

The call getIt<SettingsBloc>() works without crashes. But it shouldn't. Why missing required parameter is considered expected behavior, and it is silently replaced with null? (Untill null pointer exception thrown somewhere).

AlexanderFarkas avatar Dec 08 '20 08:12 AlexanderFarkas

This looks to me as if you are using injectable. I think get_it doesn't do anything like that.

escamoteur avatar Dec 08 '20 11:12 escamoteur

It's not the problem with injectable. I can reproduce the same in pure get_it.

Registering: GetIt.instance.registerFactoryParam<SettingsBloc, Event, dynamic>((event, _) => SettingsBloc(event));

then: final bloc = GetIt.instance<SettingsBloc>()

and: bloc.event == null

AlexanderFarkas avatar Dec 08 '20 13:12 AlexanderFarkas

ah; it's a factory with parameters. yeah, get_it can't know what value you want to pass there. i I wonder if it would help to give them default values of ´Objec()´

escamoteur avatar Dec 08 '20 14:12 escamoteur

I suppose, it should at least throw, when required param is not passed. And, of course, it shouldn't be null by default

AlexanderFarkas avatar Dec 09 '20 04:12 AlexanderFarkas

could you creat a PR with a unit test for this?

escamoteur avatar Dec 09 '20 13:12 escamoteur

@escamoteur Any solutions to this?

sannykhan3777 avatar Jan 21 '24 14:01 sannykhan3777