Dart Uri Api - 请求通过将 HTML 代码提供给问号来更改 link

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

我想从 API 获取一些数据,link 包含一个问号,使用 Uri 的请求更改了 link 所以它无法访问我的 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');
  }
}

打印结果:

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

?%3F

所以请求加载数据失败

Results

构建查询地图

 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);
//..

只需使用 Uri.parse 而不是 Uri.https。它更简单,更不容易出错。

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