【问题标题】:I have this issue when I am using http package in flutter {The argument type 'String' can't be assigned to the parameter type 'Uri'} [closed]当我在flutter中使用http包时遇到这个问题{参数类型'String'不能分配给参数类型'Uri'} [关闭]
【发布时间】:2021-06-02 22:07:29
【问题描述】:

**我在使用 get 方法时遇到了这个问题。 这是我的加载屏幕包含 http.get 方法的第一个参数的错误 **

import 'dart:convert';

class NetworkHelper {
final String url;
NetworkHelper(this.url);

Future getData() async {
 final response = await http.get(url);
 if (response.statusCode == 200) {
   String data = response.body;
   return jsonDecode(data);
 } else {
   print(response.statusCode);
 }
}
} `

【问题讨论】:

  • 请添加更多关于您的错误的描述,如果您提供描述会很棒。

标签: api flutter http


【解决方案1】:

您必须使用 Uri :

https://flutter.dev/docs/cookbook/networking/fetch-data

Future getData() async {
 final response = await http.get(Uri.https(url));
 if (response.statusCode == 200) {
   String data = response.body;
   return jsonDecode(data);
 } else {
   print(response.statusCode);
 }
}
}

【讨论】:

    【解决方案2】:

    使用Uri.parse() 将字符串转换为 URL。

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 2021-11-23
      • 2021-06-27
      • 2021-06-19
      • 2021-06-30
      • 2022-10-12
      • 2022-11-17
      • 1970-01-01
      相关资源
      最近更新 更多