首页 > 解决方案 > Flutter Injectable:通过注解注入多个泛型类型实例

问题描述

我在我的颤振项目中使用 get_it 进行依赖注入。现在我正在尝试用可注入的方法来用注释替换我的手写依赖文件。

我有一个特殊情况,我有一个通用的类,必须用不同的 T 值注入 3 次。

class TestBloc<T> {
    ...
}

这是我的旧配置中的样子:

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

我怎样才能注释类以使其工作?

当我添加@injectable

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

我明白了(当然):

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

如何注释才能得到这个?

gh.factory<_i34.TestBloc<One>>(() =>_i34.TestBloc<One>(...);
gh.factory<_i34.TestBloc<Two>>(() =>_i34.TestBloc<Two>(...);
gh.factory<_i34.TestBloc<Three>>(() =>_i34.TestBloc<Three>(...);

标签: fluttergenericsdependency-injectioninjectable

解决方案


推荐阅读