execute method Null safety

Stream<OtaEvent> execute(
  1. String url,
  2. {Map<String, String> headers = const <String, String>{},
  3. String? androidProviderAuthority,
  4. String? destinationFilename,
  5. String? sha256checksum}
)

Execute download and instalation of the plugin. Download progress and all success or error states are publish in stream as OtaEvent

Implementation

Stream<OtaEvent> execute(
  String url, {
  Map<String, String> headers = const <String, String>{},
  String? androidProviderAuthority,
  String? destinationFilename,
  String? sha256checksum,
}) {
  if (destinationFilename != null && destinationFilename.contains('/')) {
    throw OtaUpdateException('Invalid filename $destinationFilename');
  }
  final StreamController<OtaEvent> controller =
      StreamController<OtaEvent>.broadcast();
  if (_progressStream == null) {
    _progressChannel.receiveBroadcastStream(
      <dynamic, dynamic>{
        'url': url,
        'androidProviderAuthority': androidProviderAuthority,
        'filename': destinationFilename,
        'checksum': sha256checksum,
        'headers': jsonEncode(headers)
      },
    ).listen((dynamic event) {
      final OtaEvent otaEvent = _toOtaEvent(event.cast<String>());
      controller.add(otaEvent);
      if (otaEvent.status != OtaStatus.DOWNLOADING) {
        controller.close();
      }
    }).onError((Object error) {
      if (error is PlatformException) {
        controller.add(_toOtaEvent(<String?>[error.code, error.message]));
      }
    });
    _progressStream = controller.stream;
  }
  return _progressStream!;
}