使用@factoryParam getIt
DI getIt with @factoryParam
这可能是一个愚蠢的问题,但我不明白我应该如何使用 GetIt 将参数传递给 DI class in flutter (dart)。
样本:
@injectable
class Bloc
extends Bloc<Event, State> {
Bloc(@factoryParam String url)
: super(const ShowHideColumnBullListState.initial());
//more code
}
带有 creating/retrieving Bloc 的代码段:
Widget _buildBlocProviderWidget(BuildContext context) {
const event = Event.didStart();
return BlocProvider(
create: (context) => getIt<Bloc>()..add(event),//what should I call to pass String as a parameter? I cannot pass String to constructor call ()
child: _buildBlocConsumerWidget(context),
);
}
我想你正在寻找这个:
getIt<Bloc>(param1: url,);
来自 get_it (Passing Parameters to factories)
/// registers a type so that a new instance will be created on each call of [getAsync]
/// on that type based on up to two parameters provided to [getAsync()]
/// the creation function is executed asynchronously and has to be accessed with [getAsync]
/// [T] type to register
/// [P1] type of param1
/// [P2] type of param2
/// if you use only one parameter pass void here
/// [func] factory function for this type that accepts two parameters
/// [instanceName] if you provide a value here your factory gets registered with that
/// name instead of a type. This should only be necessary if you need to register more
/// than one instance of one type. Its highly not recommended
///
/// example:
/// getIt.registerFactoryParam<TestClassParam,String,int>((s,i) async
/// => TestClassParam(param1:s, param2: i));
///
/// if you only use one parameter:
///
/// getIt.registerFactoryParam<TestClassParam,String,void>((s,_) async
/// => TestClassParam(param1:s);
@override
void registerFactoryParamAsync<T, P1, P2>(
FactoryFuncParamAsync<T, P1, P2> func,
{String instanceName}) {
_register<T, P1, P2>(
type: _ServiceFactoryType.alwaysNew,
instanceName: instanceName,
factoryFuncParamAsync: func,
isAsync: true,
shouldSignalReady: false);
}
这可能是一个愚蠢的问题,但我不明白我应该如何使用 GetIt 将参数传递给 DI class in flutter (dart)。
样本:
@injectable
class Bloc
extends Bloc<Event, State> {
Bloc(@factoryParam String url)
: super(const ShowHideColumnBullListState.initial());
//more code
}
带有 creating/retrieving Bloc 的代码段:
Widget _buildBlocProviderWidget(BuildContext context) {
const event = Event.didStart();
return BlocProvider(
create: (context) => getIt<Bloc>()..add(event),//what should I call to pass String as a parameter? I cannot pass String to constructor call ()
child: _buildBlocConsumerWidget(context),
);
}
我想你正在寻找这个:
getIt<Bloc>(param1: url,);
来自 get_it (Passing Parameters to factories)
/// registers a type so that a new instance will be created on each call of [getAsync]
/// on that type based on up to two parameters provided to [getAsync()]
/// the creation function is executed asynchronously and has to be accessed with [getAsync]
/// [T] type to register
/// [P1] type of param1
/// [P2] type of param2
/// if you use only one parameter pass void here
/// [func] factory function for this type that accepts two parameters
/// [instanceName] if you provide a value here your factory gets registered with that
/// name instead of a type. This should only be necessary if you need to register more
/// than one instance of one type. Its highly not recommended
///
/// example:
/// getIt.registerFactoryParam<TestClassParam,String,int>((s,i) async
/// => TestClassParam(param1:s, param2: i));
///
/// if you only use one parameter:
///
/// getIt.registerFactoryParam<TestClassParam,String,void>((s,_) async
/// => TestClassParam(param1:s);
@override
void registerFactoryParamAsync<T, P1, P2>(
FactoryFuncParamAsync<T, P1, P2> func,
{String instanceName}) {
_register<T, P1, P2>(
type: _ServiceFactoryType.alwaysNew,
instanceName: instanceName,
factoryFuncParamAsync: func,
isAsync: true,
shouldSignalReady: false);
}