getMediaStream static method

Stream<List<SharedMediaFile>> getMediaStream(
  1. String identifier
)

Sets up a broadcast stream for receiving incoming media share change events.

Returns a broadcast Stream which emits events to listeners as follows:

  • a decoded data (List) event (possibly null) for each successful event received from the platform plugin;
  • an error event containing a PlatformException for each error event received from the platform plugin.

Errors occurring during stream activation or deactivation are reported through the FlutterError facility. Stream activation happens only when stream listener count changes from 0 to 1. Stream deactivation happens only when stream listener count changes from 1 to 0.

If the app was started by a link intent or user activity the stream will not emit that initial one - query either the getInitialMedia instead.

Implementation

static Stream<List<SharedMediaFile>> getMediaStream(String identifier) {
  if (_streamMedia == null) {
    final stream =
    _eChannelMedia.receiveBroadcastStream(identifier).cast<String?>();
    _streamMedia = stream.transform<List<SharedMediaFile>>(
      new StreamTransformer<String?, List<SharedMediaFile>>.fromHandlers(
        handleData: (String? data, EventSink<List<SharedMediaFile>> sink) {
          if (data == null) {
            sink.add([]);
          } else {
            final encoded = jsonDecode(data);
            sink.add(encoded
                .map<SharedMediaFile>(
                    (file) => SharedMediaFile.fromJson(file))
                .toList());
          }
        },
      ),
    );
  }
  return _streamMedia!;
}