streamHotspotInfo method

  1. @override
Stream<HotspotHostState> streamHotspotInfo()
override

Provides a broadcast stream of HotspotHostState updates.

This stream emits events whenever the host's Wi-Fi Direct group status changes (e.g., created, failed, IP address assigned). Being a broadcast stream, it supports multiple listeners simultaneously. Each listener will receive events emitted after it subscribes.

Returns a Stream of HotspotHostState.

Implementation

@override
Stream<HotspotHostState> streamHotspotInfo() {
  _hotspotInfoStream ??= hotspotStateEventChannel
      .receiveBroadcastStream()
      .map((dynamic event) {
    // Ensure the event is a Map before attempting to parse
    if (event is Map) {
      try {
        return HotspotHostState.fromMap(Map.from(event));
      } catch (e) {
        debugPrint(
            "[MethodChannelFlutterP2pConnection] Error parsing HotspotHostState: $e, Event: $event");
        // Depending on requirements, you might want to return a default error state
        // or filter out invalid events. Here, we rethrow to signal a parsing issue.
        rethrow;
      }
    } else {
      debugPrint(
          "[MethodChannelFlutterP2pConnection] Received non-map event on hotspotStateEventChannel: $event");
      // Filter out unexpected event types by returning null or throwing
      // For robustness, let's throw an error indicating unexpected data.
      throw FormatException(
          "Received unexpected data type on hotspotStateEventChannel: ${event.runtimeType}");
    }
  })
      // Ensure the resulting stream is broadcast (map usually preserves it, but this is explicit)
      .asBroadcastStream();
  return _hotspotInfoStream!;
}