fetch function

void fetch({
  1. required String url,
  2. required String saveToPath,
  3. FetchMethod method = FetchMethod.get,
  4. Map<String, String>? headers,
  5. OnFetchProgress fetchProgress = _devNull,
  6. FetchData? data,
})

Fetches the given resource at the passed url.

 print(''); // will be overwritten with progress messages.
 fetch(
     url:
         'https://some/resource/file.zip',
    saveToPath: pathToPiImage,
    fetchProgress: FetchProgress.showBytes
        });

The url must be a http or https based resource.

The saveToPath may be an absolute (recommended) or relative path where to save the downloaded resource.

The file at saveToPath must NOT exist. If it does a FetchException will be thrown.

Any headers that you pass are sent as HTTP headers along with their value.

When using the FetchMethod.post you provide a data argument which supplies the data to be sent. If you set data for any other method then a

You may optionally passing in a fetchProgress method which will be called each time a chunk is downloaded with details on the download progress. We guarentee that you will recieve a final event with the FetchProgress.progress containing a value of 1.0 and a status of 'complete'.

In the future we MAY allow you to cancel the download part way through by returning false to the fetchProgress call. In the meantime ensure that you always return true from the 'onProgress' callback.

Throws a FetchException if something goes wrong. In the event of a HTTP error we will still try to download the body and save it to saveToPath as often the body contains additional information about the error.

Implementation

void fetch({
  required String url,
  required String saveToPath,
  FetchMethod method = FetchMethod.get,
  Map<String, String>? headers,
  OnFetchProgress fetchProgress = _devNull,
  FetchData? data,
}) {
  headers ??= <String, String>{};
  _Fetch().fetch(
      url: url,
      saveToPath: saveToPath,
      method: method,
      headers: headers,
      progress: fetchProgress,
      data: data);
}