【问题标题】:Getting error for Flutter http.get('url') parameter It's not accepting Url StringFlutter http.get('url') 参数出现错误它不接受 URL 字符串
【发布时间】:2021-06-29 14:40:24
【问题描述】:

我正在尝试获取 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=/"

【问题讨论】:

    标签: flutter flutter-dependencies flutter-test flutter-http


    【解决方案1】:

    添加: 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"];
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 即使对于 res=await http.get(Uri.parse(api)); 也会引发错误"连接服务协议时出错:无法连接到127.0.0.1:64971/ypwDk3yQ650="
      • 看来这根本不是代码问题! stackoverflow.com/questions/53933251/…
      • 是的,先生,这不是代码问题,借助此处提供的答案解决了它stackoverflow.com/questions/53933251/…
      • 这很奇怪,但它在 1)我的笔记本电脑与 wifi 断开连接 2)从笔记本电脑上拔下 USB 电缆和手机再次将笔记本电脑连接到 wifi 3)将手机连接到笔记本电脑并运行应用程序
      • 但是这一切都是在将 Uri.Parse(api) 传递给 http.get(Uri.Parse(api)) 之后发生的,谢谢。
      猜你喜欢
      • 2018-10-11
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多