这可能是一个愚蠢的问题,但我不知道如何使用 GetIt 将参数传递给 flutter (dart) 中的 DI 类。
样本:
@injectable
class Bloc
extends Bloc<Event, State> {
Bloc(@factoryParam String url)
: super(const ShowHideColumnBullListState.initial());
//more code
}
Run Code Online (Sandbox Code Playgroud)
创建/检索 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),
);
}
Run Code Online (Sandbox Code Playgroud)
我想你正在寻找这个:
getIt<Bloc>(param1: url,);
Run Code Online (Sandbox Code Playgroud)
/// 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3067 次 |
| 最近记录: |