Skip to content

registerFactoryParam with more than 2 input params #217

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
mashahbazi opened this issue Jul 27, 2021 · 2 comments
Closed

registerFactoryParam with more than 2 input params #217

mashahbazi opened this issue Jul 27, 2021 · 2 comments

Comments

@mashahbazi
Copy link

mashahbazi commented Jul 27, 2021

Hi
It is a good idea to have this registerFactoryParam. It was a missing part of get_it to become a complete DI tools for Flutter. There is still some improvement that we can do here. Right now get_it doesn't support constructor injection very well and you have to use property injection (constructor injection in some cases like testing is a better practice than property injection).
The registerFactoryParam can be a good solution for solving this problem but it's input parameters now has limits. This limit can cause problem in cases like this.
Here is a sample code works perfectly, but if your input parameters can't be more than 2 which limits this.

class A {}

class B {}

class C {
  A a;
  B b;
  int num1;
  int num2;

  C(this.a, this.b, this.num1, this.num2);
}

main() {
  GetIt.I.registerFactory<A>(() => A());
  GetIt.I.registerFactory<B>(() => B());
  GetIt.I.registerFactoryParam<C, int, int>(
      (param1, param2) => C(GetIt.I<A>(), GetIt.I<B>(), param1, param2));

  C c = GetIt.I.get<C>(param1: 1, param2: 2);
}
@escamoteur
Copy link
Collaborator

Hi, sorry for not looking into this earlier, but I had some mental health problems the last half year.

Did you have a look at the injectable package that uses get_it ?

@Rohithgilla12
Copy link

If someone stumbles around this.
One hacky way to get around this is to create a class with the elements you want, and pass in the class.

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

3 participants