无法将参数类型“String”分配给参数类型“Uri”

时间:2021-03-04 10:33:45

标签: flutter dart

我正在尝试使用 flutter 插件 HTTP 发出 HTTP POST 请求,但出现标题错误。 有没有人知道这是什么原因,因为在我的其他应用程序中这工作得很好?

await http.post(Uri.encodeFull("https://api.instagram.com/oauth/access_token"), body: {
      "client_id": clientID,
      "redirect_uri": redirectUri,
      "client_secret": appSecret,
      "code": authorizationCode,
      "grant_type": "authorization_code"
    });

2 个答案:

答案 0 :(得分:82)

为了提高编译时类型安全性,package:http 0.13.0 introduced breaking changes 使之前接受 UriString 的所有函数现在只接受 {{1} } 代替。您将需要明确使用 Uri.parseUri 创建 Uri。 (String 以前在内部为您调用过。)

<头>
旧代码 替换为
package:http http.get(someString)
http.get(Uri.parse(someString)) http.post(someString)

(等等。)

在您的具体示例中,您需要使用:

http.post(Uri.parse(someString))

答案 1 :(得分:1)

如果你不想使用 parse 而想用简单的方法,这里是例子:

假设您想从以下位置获取一些数据:- https://example.com/helloworld

它对我有用:

String url ='example.com';
int helloworld=5;
http.client;
client.get(Uri.https(url,'/$helloworld'),
  headers:{Content-type':'application/json',
  },
 );

如果你想得到: http://example.com/helloworld

代替

https://example.com/helloworld

您只需更改 Uri.http(),仅此而已。

这是正确的。 http.gethttp.post 现在接受 only Uris ,但正如您所见,这没什么大不了的。