You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter:Injectable包无法为类中多个工厂方法生成代码问题

问题:使用Injectable + GetIt时,多个命名构造函数仅生成一个工厂方法

我在使用GetIt和Injectable做依赖注入,希望通过两个命名构造函数(firebase和google)创建AuthBloc的不同实例,但生成的DI代码里只包含了firebase构造函数对应的工厂方法,google的构造函数完全没生成。

原代码

@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());
}

生成的代码(仅包含一个工厂方法)

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;
}

解决方法

要让Injectable为每个命名构造函数生成对应的工厂方法,必须给每个@factoryMethod指定唯一的instanceName,否则Injectable会默认覆盖同类型的工厂方法。

修改后的AuthBloc代码:

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

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

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

重新生成DI代码后,会看到两个带instanceName的AuthBloc工厂方法:

gh.factory<_i14.AuthBloc>(
  () => _i14.AuthBloc.firebase(gh<_i9.AuthManager<_i10.UserData>>(instanceName: 'FirebaseAuthManager')),
  instanceName: 'firebase'
);
gh.factory<_i14.AuthBloc>(
  () => _i14.AuthBloc.google(gh<_i9.AuthManager<_i10.UserData>>(instanceName: 'GoogleAuthManager')),
  instanceName: 'google'
);

使用时通过指定instanceName获取对应实例:

// 获取Firebase版本的AuthBloc
final firebaseAuthBloc = getIt<AuthBloc>(instanceName: 'firebase');
// 获取Google版本的AuthBloc
final googleAuthBloc = getIt<AuthBloc>(instanceName: 'google');

内容的提问来源于stack exchange,提问作者Rishabh Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:47:17