enqueue static method
Creates a new task which downloads a file from url
to savedDir
and
returns a unique identifier of that new download task.
Name of the downloaded file is determined from the HTTP response and from
the url
. Set fileName
if you want a custom filename.
savedDir
must be an absolute path.
headers
are HTTP headers that will be sent with the request.
Android-only
If showNotification
is true, a notification with the current download
progress will be shown.
If requiresStorageNotLow
is true, the download won't run unless the
device's available storage is at an acceptable level.
If openFileFromNotification
is true, the user can tap on the
notification to open the downloaded file. If it is false, nothing happens
when the tapping the notification.
Android Q (API 29) changed the APIs for accessing external storage. This
means that apps must store their data in an app-specific directory on
external storage. If you want to save the file in the public Downloads
directory instead, set saveInPublicStorage
to true. In that case,
savedDir
will be ignored.
timeout
is the HTTP connection timeout.
Implementation
static Future<String?> enqueue({
required String url,
required String savedDir,
String? fileName,
Map<String, String> headers = const {},
bool showNotification = true,
bool openFileFromNotification = true,
bool requiresStorageNotLow = true,
bool saveInPublicStorage = false,
bool allowCellular = true,
int timeout = 15000,
}) async {
assert(_initialized, 'plugin flutter_downloader is not initialized');
assert(Directory(savedDir).existsSync(), 'savedDir does not exist');
try {
final taskId = await _channel.invokeMethod<String>('enqueue', {
'url': url,
'saved_dir': savedDir,
'file_name': fileName,
'headers': jsonEncode(headers),
'show_notification': showNotification,
'open_file_from_notification': openFileFromNotification,
'requires_storage_not_low': requiresStorageNotLow,
'save_in_public_storage': saveInPublicStorage,
'timeout': timeout,
'allow_cellular': allowCellular,
});
if (taskId == null) {
throw const FlutterDownloaderException(
message: '`enqueue` returned null taskId',
);
}
return taskId;
} on FlutterDownloaderException catch (err) {
_log('Failed to enqueue. Reason: ${err.message}');
} on PlatformException catch (err) {
_log('Failed to enqueue. Reason: ${err.message}');
}
return null;
}