restore method

  1. @override
void restore(
  1. Object state
)
override

Puts every part back to what state, a capture of this registry, holds.

Throws a StateError when state holds no part this registry has: a restore that restored nothing is a rewind that silently stayed where it was, which is the failure this registry exists to rule out.

Implementation

@override
void restore(Object state) {
  final held = state is Map ? state : const <Object?, Object?>{};
  final read = <(SnapshotPart, SnapshotRestoreContext)>[
    for (final part in _parts)
      if (held[part.id] case final Map<Object?, Object?> entry)
        (part, _contextFor(part, entry)),
  ];
  if (read.isEmpty && _parts.isNotEmpty) {
    throw StateError(
      'the snapshot restores none of the parts: it holds '
      '${held.keys.isEmpty ? 'nothing' : held.keys.join(', ')} and the '
      'parts are ${parts.join(', ')}',
    );
  }
  for (final first in <bool>[true, false]) {
    for (final (part, context) in read) {
      if (part.restoresFirst == first) part.restore(context);
    }
  }
}