downloadFileFromUrl static method

Future<void> downloadFileFromUrl(
  1. String url
)

Implementation

static Future<void> downloadFileFromUrl(String url) async {
  try {
    final path = await DeviceDirectoriesManager.instance.downloadDirectoryPath();

    final isolatePort = ReceivePort();
    IsolateNameServer.removePortNameMapping(
      'downloader_send_port',
    );
    IsolateNameServer.registerPortWithName(
      isolatePort.sendPort,
      'downloader_send_port',
    );

    isolatePort.listen(
      (data) async {
        final id = (data as List<dynamic>)[0];
        final status = data[1];

        if (Platform.isIOS && status == 3) {
          await Future<void>.delayed(const Duration(seconds: 1));

          await FlutterDownloader.open(taskId: id as String);
        }
      },
      onError: (error) {},
    );

    LoggerService.logInfo('downloadFileFromUrl to path: $path');

    await FlutterDownloader.registerCallback(
      downloadCallback,
    );

    final taskId = await FlutterDownloader.enqueue(
      url: url,
      savedDir: path ?? '',
      saveInPublicStorage: true,
    );

    LoggerService.logInfo('downloadFileFromUrl finished taskId: $taskId');
  } catch (e) {
    LoggerService.logError('downloadFileFromUrl error: $e');
  }
}