single<VM extends ViewModel> static method

ViewModelConsumer single<VM extends ViewModel>({
  1. Key? key,
  2. required ViewModelFactory<VM> creator,
  3. ViewModelConsumerBuilder<VM>? builder,
  4. Widget? child,
  5. ViewModelShareStrategy shareStrategy = ViewModelShareStrategy.none,
})

创建一个 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,
  );
}