watch<T> method

  1. @protected
  2. @nonVirtual
void watch<T>(
  1. Repository<T> repository
)

Watches another repository for changes.

Call this method in the constructor of your repository and override the build method to handle changes.

Example:

class MyRepository extends Repository<MyState> {
  final OtherRepository _otherRepository;

  MyRepository(this._otherRepository) : super(MyState.new) {
    watch(_otherRepository);
  }

  @override
  FutureOr<void> build(Type trigger) async {
    final otherState = _otherRepository.state;

    // Do something with otherState
  }
}

For watching repositories that return an AsyncValue, use RepoWatchExt.watchAsync. Note: The repository watching must also return an AsyncValue to use RepoWatchExt.watchAsync.

Implementation

@protected
@nonVirtual
void watch<T>(Repository<T> repository) {
  assert(repository != this, 'Cannot watch self');

  _subscriptions.add(
    repository.stream.listen(
      (value) => _didChangeDependencies<T>(
        value,
        repository,
      ),
    ),
  );
}