get method

Future<Response> get(
  1. String url, {
  2. Map<String, String>? headers,
  3. String responseDecoder(
    1. Uint8List
    )?,
  4. Map<String, String>? queryParams,
})

Effettua una chiamata GET

Implementation

Future<Response> get(
  String url, {
  Map<String, String>? headers,
  String Function(Uint8List)? responseDecoder,
  Map<String, String>? queryParams,
}) async {
  String urlToCall = url;

  try {
    /// Aggiunge i queryParams
    if (queryParams != null && queryParams.isNotEmpty) {
      // ignore: parameter_assignments
      urlToCall =
          // ignore: lines_longer_than_80_chars
          "$url?${queryParams.entries.map((e) => "${e.key}=${e.value}").reduce((value, element) => "$value&$element")}";
    }

    final response =
        await _client.get(Uri.parse(urlToCall), headers: headers);

    if (responseDecoder != null) {
      return Response(
        responseDecoder.call(response.bodyBytes),
        response.statusCode,
      );
    }

    _printLog(urlToCall, response, 'GET');
    return response;
  } catch (e) {
    logger.w('[Provider-GET] Error on url $urlToCall: $e');
    return Response(jsonEncode({'code': 'GENERIC_ERROR'}), 500);
  }
}