fetch function

Future<Response> fetch(
  1. String url, {
  2. FetchOption? options,
  3. Encoding? encoding,
})

Implementation

Future<Response> fetch(
  String url, {
  FetchOption? options,
  Encoding? encoding,
}) async {
  options ??= FetchOption(
    method: "get",
  );
  String method = (options.method).toLowerCase();

  Response response;
  if (method == "get") {
    response = await get(
      Uri.parse(url),
      headers: options.headers,
    );
  } else if (method == "post") {
    response = await post(Uri.parse(url),
        body: options.body, headers: options.headers, encoding: encoding);
  } else if (method == "put") {
    response = await put(Uri.parse(url),
        body: options.body, headers: options.headers, encoding: encoding);
  } else if (method == "patch") {
    response = await patch(Uri.parse(url),
        body: options.body, headers: options.headers, encoding: encoding);
  } else if (method == "delete") {
    response = await delete(Uri.parse(url),
        body: options.body, headers: options.headers, encoding: encoding);
  } else if (method == "head") {
    response = await head(Uri.parse(url), headers: options.headers);
  } else {
    response = await get(
      Uri.parse(url),
      headers: options.headers,
    );
  }

  return response;
}