0

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;
}
Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38

1 Answers1

0

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.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 08 '23 at 07:28