Dart Uri Api – The request changed the link by giving the HTML code to the question mark

Issue

I want to get some data from an API, the link contains a question mark, the request using the Uri changed the link so it fails to access to my link.

Uri url = Uri.https('api.aladhan.com',
    'v1/calendar?latitude=45.53157194849857&longitude=9.13990990079155&method=2&month=5&year=2021');
Future<MonthPrayers> fetchAlbum() async {
  final response = await http.get(url);

  if (response.statusCode == 200) {
    return MonthPrayers.fromJson(jsonDecode(response.body));
  } else {
    print("Uri : " + url.toString());
    throw Exception('Failed to load album');
  }
}

Result of the print :

Uri : https://api.aladhan.com/v1/calendar%3Flatitude=45.53157194849857&longitude=9.13990990079155&method=2&month=5&year=2021

? to %3F

So the request fails to load the data

Results

Solution

Just use Uri.parse instead of Uri.https. It is much simpler and less error-prone.

Uri url = Uri.parse('https://api.aladhan.com/v1/calendar?latitude=45.53157194849857&longitude=9.13990990079155&method=2&month=5&year=2021');

Answered By – jamesdlin

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *