不能将参数类型‘ String’分配给参数类型‘ Uri’

我正在尝试使用 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"
});
97507 次浏览

To improve compile-time type safety, package:http 0.13.0 introduced breaking changes that made all functions that previously accepted Uris or Strings now accept only Uris instead. You will need to explicitly use Uri0 to create Uris from Strings. (package:http formerly called that internally for you.)

Old Code Replace With
http.get(someString) http.get(Uri.parse(someString))
http.post(someString) http.post(Uri.parse(someString))

(and so on.)

In your specific example, you will need to use:

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",
});

Edit:

Since I'm still getting upvotes on this answer over a year later, it seems that there are still many people encountering this problem, probably from outdated tutorials. If so, while I appreciate the upvotes, I strongly recommend leaving comments on those tutorials to request that they be updated.

String url ='example.com';


http.get(Uri.parse(url),


);

Try this one.

http.post(urlstring) replace with http.post(Uri.parse(urlstring))

 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"
});