clone method Null safety

Future<Video> clone()

Clone this video instance and the underlying HTML VideoElement to play the video independantly from this video.

Implementation

Future<Video> clone() {
  final videoElement = this.videoElement.clone(true) as VideoElement;
  final completer = Completer<Video>();
  late StreamSubscription onCanPlaySubscription;
  late StreamSubscription onErrorSubscription;

  void onCanPlay(html.Event e) {
    final video = Video._(videoElement);
    video.volume = volume;
    video.muted = muted;
    onCanPlaySubscription.cancel();
    onErrorSubscription.cancel();
    completer.complete(video);
  }

  void onError(html.Event e) {
    onCanPlaySubscription.cancel();
    onErrorSubscription.cancel();
    final error = videoElement.error;
    final loadError = LoadError('Failed to clone video.', error);
    completer.completeError(loadError);
  }

  onCanPlaySubscription = videoElement.onCanPlay.listen(onCanPlay);
  onErrorSubscription = videoElement.onError.listen(onError);
  return completer.future;
}