asyncDownload method

ProgressListener asyncDownload(
  1. void onComplete(
    1. GemError err
    ), {
  2. void onProgress(
    1. int progress
    )?,
  3. bool allowChargedNetworks = false,
  4. DataSavePolicy savePolicy = DataSavePolicy.useDefault,
  5. ContentDownloadThreadPriority priority = ContentDownloadThreadPriority.defaultPriority,
})

Start or resume an asynchronous download of the content product.

The method creates and registers a ProgressListener which will receive progress and completion events. The caller must provide a mandatory onComplete callback to receive the final outcome. When the operation finishes the listener's completion callback and the supplied onComplete are invoked with a GemError indicating success or the failure reason.

The callback can be changed later using setProgressListener or retrieved via the progressListener property.

Parameters

Returns

  • A ProgressListener instance associated with the download. The listener reports progress updates and completion. If the operation cannot be started an error is reported via the onComplete callback and a placeholder listener is returned.

Example

The following example starts a download and logs progress and completion:

item.asyncDownload((GemError err) {
  if (err == GemError.success) {
    print('Download finished successfully');
  } else {
    print('Download failed: $err');
  }
}, onProgress: (int p) {
  print('Download progress: $p%');
});

Also see:

Implementation

ProgressListener asyncDownload(
  final void Function(GemError err) onComplete, {
  final void Function(int progress)? onProgress,
  final bool allowChargedNetworks = false,
  final DataSavePolicy savePolicy = DataSavePolicy.useDefault,
  final ContentDownloadThreadPriority priority =
      ContentDownloadThreadPriority.defaultPriority,
}) {
  final EventDrivenProgressListener progListener =
      EventDrivenProgressListener();
  final OperationResult resultString = objectMethod(
    pointerId,
    'ContentStoreItem',
    'asyncDownload',
    args: <String, dynamic>{
      'listener': progListener.id,
      'allowChargedNetworks': allowChargedNetworks,
      'savePolicy': savePolicy.id,
      'priority': priority.id,
    },
  );

  if (resultString.containsKey('error')) {
    onComplete(GemErrorExtension.fromCode(resultString['error']));
    return EventDrivenProgressListener.init(0);
  }

  if (onProgress != null) {
    progListener.registerOnProgress((final int p0) {
      onProgress.call(p0);
    });
  }

  progListener.registerOnCompleteWithData((
    final int err,
    final String hint,
    final Map<dynamic, dynamic> json,
  ) {
    if (err == 0) {
      onComplete(GemErrorExtension.fromCode(0));
    } else {
      onComplete(GemErrorExtension.fromCode(err));
    }
  });
  GemKitPlatform.instance.registerEventHandler(progListener.id, progListener);
  return progListener;
}