SystemEvent.fromMap constructor

SystemEvent.fromMap(
  1. Map map
)

Decodes an event from a platform channel map.

Implementation

factory SystemEvent.fromMap(Map<dynamic, dynamic> map) {
  try {
    return switch (map['type']) {
      'keyboard' => KeyboardEvent(
        visible: map['visible'] as bool,
        height: (map['height'] as num).toDouble(),
      ),
      'lifecycle' => LifecycleEvent(
        state: LifecycleState.values.byName(map['state'] as String),
      ),
      'network' => NetworkEvent(
        online: map['online'] as bool,
        networkType: NetworkType.values.byName(map['networkType'] as String),
      ),
      'memory' => MemoryEvent(
        state: MemoryState.values.byName(map['state'] as String),
        level: map['level'] as int,
      ),
      'battery' => BatteryEvent(
        level: map['level'] as int,
        charging: map['charging'] as bool,
        state: BatteryState.values.byName(map['state'] as String),
      ),
      'orientation' => OrientationEvent(
        orientation: ScreenOrientation.values.byName(
          map['orientation'] as String,
        ),
      ),
      'time' => TimeEvent(
        reason: TimeChangeReason.values.byName(map['reason'] as String),
      ),
      'screen' => ScreenEvent(
        change: ScreenChange.values.byName(map['change'] as String),
        brightness: map['change'] == 'brightness'
            ? (map['brightness'] as num).toDouble()
            : null,
      ),
      _ => UnknownSystemEvent(
        rawPayload: map,
        rawType: map['type'],
        reason: 'Unsupported system event type.',
      ),
    };
  } on Object catch (error) {
    return UnknownSystemEvent(
      rawPayload: map,
      rawType: map['type'],
      reason: 'Invalid system event payload: $error',
    );
  }
}