register method

void register(
  1. String key,
  2. Map<String, dynamic> exporter(),
  3. void importer(
    1. Map<String, dynamic>
    )
)

Registers a state handler with the given key.

  • key: Unique string identifier for this state contributor
  • exporter: Function that exports state as Map<String, dynamic>
  • importer: Function that imports state from Map<String, dynamic>

Throws StateError if the key is already registered.

Thread-safe: This operation is synchronized.

Implementation

void register(
  String key,
  Map<String, dynamic> Function() exporter,
  void Function(Map<String, dynamic>) importer,
) {
  if (key.isEmpty) {
    throw ArgumentError.value(key, 'key', 'Key cannot be empty');
  }

  synchronized(() {
    if (_handlers.containsKey(key)) {
      throw StateError(
        'State handler with key "$key" is already registered. '
        'Keys must be unique.',
      );
    }

    _handlers[key] = _StateHandler(exporter: exporter, importer: importer);
  });
}