获取 Flutter http.get('url') 参数错误它不接受 Url 字符串

Getting error for Flutter http.get('url') parameter It's not accepting Url String

我正在尝试获取 https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail api returns 的所有 json 数据,以便在我的 flutter 应用程序中使用它

var api="https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail";
var res=await http.get(api);
drinks= jsonDecode(res.body)["drinks"];

但它在 http.get(api) 函数调用中的 api 参数下给出了红色下划线,表示“字符串类型的参数无法转换为 URI” 如何解决这个问题?我什至尝试使用 res=await http.get(Uri.https('www.thecocktaildb.com','/api/json/v1/1/filter.php?c=Cocktail'));

但出现错误“连接到服务协议时出错:无法连接到 http://127.0.0.1:63573"/_fprblq_tiY=/"

不要使用 Uri.https(),请尝试使用 Uri.parse()

添加: http: ^0.13.4 到您的 pubspec.yaml ,它将为您提供一些访问 api 的简单方法。 您不能直接添加 ? 因为它会转换为 3%F 我相信,其他特殊字符也是如此。

var url = 'thecocktaildb.com';
var urlExtension = '/api/json/v1/1/filter.php';
final Map<String, String> queryParameters = <String, String>{
  'c': 'Cocktail',
};
final api = Uri.https(url, urlExtension, queryParameters);

//It should be something like this, now you can get
//the api response the way you were doing:

var res=await http.get(api);
drinks= jsonDecode(res.body)["drinks"];