无法将参数类型“String”分配给参数类型“Uri”
我正在尝试使用颤振插件 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"
});
回答
为了提高编译时类型安全性,package:http0.13.0 引入了重大更改,使以前接受Uris 或Strings 的所有函数现在只 接受Uris。您将需要显式使用从sUri.parse创建s。(以前在内部为您调用。)UriStringpackage:http
| 旧代码 | 用。。。来代替 |
|---|---|
http.get(someString) |
http.get(Uri.parse(someString)) |
http.post(someString) |
http.post(Uri.parse(someString)) |
(等等。)
在您的具体示例中,您将需要使用:
await http.post(
Uri.parse("https://api.instagram.com/oauth/access_token"),
body: {
"client_id": clientID,
"redirect_uri": redirectUri,
"client_secret": appSecret,
"code": authorizationCode,
"grant_type": "authorization_code",
});
- @Tayan Read the documentation. `Uri.parse` takes a single URL string. `Uri.https`/`Uri.http` *build* a `Uri` from parts (a hostname, a path, and a `Map` for query arguments). If you already have the URL as a `String`, just use `Uri.parse`; it's far less error-prone than using `Uri.https`/`Uri.http` directly.