RepositoryProvider<T> constructor

RepositoryProvider<T>({
  1. required T create(
    1. BuildContext context
    ),
  2. void dispose(
    1. T value
    )?,
  3. Key? key,
  4. Widget? child,
  5. bool? lazy,
})

Takes a create function that is responsible for creating the repository and a child which will have access to the repository via RepositoryProvider.of(context). It is used as a dependency injection (DI) widget so that a single instance of a repository can be provided to multiple widgets within a subtree.

RepositoryProvider(
  create: (context) => RepositoryA(),
  child: ChildA(),
);

Lazily creates the repository unless lazy is set to false.

RepositoryProvider(
  lazy: false,`
  create: (context) => RepositoryA(),
  child: ChildA(),
);

Repositories that manage resources which must be disposed can do so via the dispose callback.

RepositoryProvider(
 create: (context) => RepositoryA(),
 dispose: (repository) => repository.dispose(),
 child: ChildA(),
);

Implementation

RepositoryProvider({
  required T Function(BuildContext context) create,
  void Function(T value)? dispose,
  Key? key,
  Widget? child,
  bool? lazy,
}) : super(
        key: key,
        create: create,
        dispose: (_, value) => dispose?.call(value),
        child: child,
        lazy: lazy,
      );