Flutter Injectable package is not generating code for multiple factory methods in a class

673 Views Asked by At

I am trying to create instance of AuthBloc by named constructer using GetIt and Injectable (Used for Dependancy Injection) package. But the code generated is only for one factoryMethod or one named constructor.

@injectable
class AuthBloc extends Cubit<AuthStates>{
  final AuthManager<UserData> _authManager;

  @factoryMethod
  AuthBloc.firebase(@Named.from(FirebaseAuthManager)this._authManager) : super(InitialAuthState());

  @factoryMethod
  AuthBloc.google(@Named.from(GoogleAuthManager)this._authManager) : super(InitialAuthState());
}

Generated Code:

Future<_i1.GetIt> $initGetIt(
  _i1.GetIt getIt, {
  String? environment,
  _i2.EnvironmentFilter? environmentFilter,
}) async {
  final gh = _i2.GetItHelper(
    getIt,
    environment,
    environmentFilter,
  );
  final firebaseModule = _$FirebaseModule();
  gh.factory<_i3.FirebaseAuth>(() => firebaseModule.firebaseAuth);
  gh.factory<_i4.FirebaseFirestore>(() => firebaseModule.firebaseFirestore);
  await gh.factoryAsync<_i5.FirebaseService>(
    () => firebaseModule.firebaseService,
    preResolve: true,
  );
  gh.factory<_i6.MoviesRepository>(() => _i6.MoviesRepository(
        gh<_i4.FirebaseFirestore>(),
        gh<_i3.FirebaseAuth>(),
      ));
  gh.factory<_i7.PlaylistBloc>(
      () => _i7.PlaylistBloc(gh<_i6.MoviesRepository>()));
  gh.factory<_i8.SearchBloc>(() => _i8.SearchBloc(gh<_i6.MoviesRepository>()));
  gh.factory<_i9.AuthManager<_i10.UserData>>(
    () => _i11.FirebaseAuthManager(
      gh<_i3.FirebaseAuth>(),
      gh<_i4.FirebaseFirestore>(),
    ),
    instanceName: 'FirebaseAuthManager',
  );
  gh.factory<_i9.AuthManager<_i10.UserData>>(
    () => _i12.GoogleAuthManager(
      gh<_i3.FirebaseAuth>(),
      gh<_i4.FirebaseFirestore>(),
    ),
    instanceName: 'GoogleAuthManager',
  );
  gh.singleton<_i13.NavigationService>(_i13.NavigationService(
      gh<_i9.AuthManager<_i10.UserData>>(instanceName: 'FirebaseAuthManager')));
  gh.factory<_i14.AuthBloc>(() => _i14.AuthBloc.firebase(
      gh<_i9.AuthManager<_i10.UserData>>(instanceName: 'FirebaseAuthManager')));
  return getIt;
}
1

There are 1 best solutions below

1
Furkan Olkay On

Why would you want to generate two different factory methods? You can think of the factory methods in Injectable as a constructor that is called with the class. You cannot do more than one. Injectable only lets you create one.