4

I am using get_it in my flutter project for the dependency injection. Now I am trying to do it with injectable to replace my handwritten dependency file with annotations.

I have the special case, that I have a class which is generic and must be injected 3 times with a different value of T.

class TestBloc<T> {
    ...
}

This is how it looks like in my old config:

sl.registerFactory(() => TestBloc<One>(...);
sl.registerFactory(() => TestBloc<Two>(...);
sl.registerFactory(() => TestBloc<Three>(...);

How can I annotate the class to make that work?

When I add @injectable

@injectable
class TestBloc<T> {
    ...
}

I get this (of course):

gh.factory<_i34.TestBloc<dynamic>>(() =>_i34.TestBloc<dynamic>(...);

How to annotate to get this?

gh.factory<_i34.TestBloc<One>>(() =>_i34.TestBloc<One>(...);
gh.factory<_i34.TestBloc<Two>>(() =>_i34.TestBloc<Two>(...);
gh.factory<_i34.TestBloc<Three>>(() =>_i34.TestBloc<Three>(...);
A.K.
  • 568
  • 5
  • 17

0 Answers0