single<VM extends ViewModel> static method
ViewModelConsumer
single<VM extends ViewModel>({
- Key? key,
- required ViewModelFactory<
VM> creator, - ViewModelConsumerBuilder<
VM> ? builder, - Widget? child,
创建一个 ViewModelConsumer,指定了单个 ViewModelFactory
Implementation
static ViewModelConsumer single<VM extends ViewModel>({
Key? key,
required ViewModelFactory<VM> creator,
ViewModelConsumerBuilder<VM>? builder,
Widget? child,
ViewModelShareStrategy shareStrategy = ViewModelShareStrategy.none,
}) {
return ViewModelConsumer(
key: key,
creators: [creator],
shareStrategy: shareStrategy,
builder: builder != null
? (context, child) {
final viewModel = context.getViewModel<VM>();
assert(
viewModel != null,
'ViewModel of type $VM not found in context',
);
return builder(context, viewModel!, child);
}
: null,
child: child,
);
}