fetchSvg method

Future<String> fetchSvg(
  1. String url
)

Easiest way to fetch the SVG doing HTTP request

Implementation

Future<String> fetchSvg(String url) async {
  try {
    if (Avataaar.cachedUrls.containsKey(avatar.toUrl())) {
      return Avataaar.cachedUrls[avatar.toUrl()]!;
    }
    final response = await http.get(Uri.parse(url));

    if (response.statusCode == 200) {
      // If the server did return a 200 OK response,
      // then save it and parse the JSON.
      Avataaar.cachedUrls.putIfAbsent(avatar.toUrl(), () => response.body);
      return response.body;
    } else {
      // If the server did not return a 200 OK response,
      // then throw an exception.
      onError?.call(Exception('Failed to load SVG'));
      throw Exception('Failed to load SVG');
    }
  } on Exception catch (e) {
    onError?.call(e);
    rethrow;
  }
}