BleConnectionEvent.fromMap constructor

BleConnectionEvent.fromMap(
  1. Map<Object?, Object?> map
)

Creates a BleConnectionEvent from the map sent across the method channel by the native platform.

The native layer always sends a map for connection updates. The state key is resolved tolerantly via BleConnectionState.fromIdentifier (falling back to BleConnectionState.unknown for unrecognized values). A failure is constructed only when the payload actually describes a failure — that is, when the resolved state is BleConnectionState.failed, or when the state is BleConnectionState.disconnected and the payload includes error detail (a category, errorCode, errorDomain, or errorDescription).

  • Parameter map: The decoded method channel arguments describing the connection update.
  • Returns: A BleConnectionEvent with the resolved state and, where applicable, populated failure.

Implementation

factory BleConnectionEvent.fromMap(Map<Object?, Object?> map) {
  final BleConnectionState state = BleConnectionState.fromIdentifier(
    map['state'] as String?,
  );

  final bool payloadHasFailureDetail = map['category'] != null ||
      map['errorCode'] != null ||
      map['errorDomain'] != null ||
      map['errorDescription'] != null;

  final bool shouldParseFailure = state == BleConnectionState.failed ||
      (state == BleConnectionState.disconnected && payloadHasFailureDetail);

  return BleConnectionEvent(
    state: state,
    failure: shouldParseFailure ? BleConnectionFailure.fromMap(map) : null,
  );
}