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 1: shb

Build the query map

 Map<String, dynamic> params = {
     'latitude': 45.53157194849857,
     'longitude': 9.13990990079155,
     'method': 2,
     'month': 5,
     'year': 2021,
    };

var url = Uri.https('api.aladhan.com', 'v1/calendar', params);
//..


Solution 2: jamesdlin

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');