首页 > 解决方案 > Flutter http.get('url') 参数出现错误它不接受 URL 字符串

问题描述

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

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=/”

标签: flutterflutter-dependenciesflutter-testflutter-http

解决方案


而不是使用Uri.https(),尝试使用Uri.parse()


推荐阅读