The argument type 'String' can't be assigned to the parameter type 'Uri'

FlutterHttpDart

Flutter Problem Overview


I am trying to make an HTTP POST request with the flutter plugin HTTP but I am getting an error of the title. Does anyone know the cause of this since in my other applications this works just perfectly fine?

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

Flutter Solutions


Solution 1 - Flutter

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 Uri.parse 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.

Solution 2 - Flutter

String url ='example.com';

http.get(Uri.parse(url),

);

Solution 3 - Flutter

You need to parse your URL By using the Uri.parse() method, you can create a new Uri object by parsing a URI string.

import 'package:http/http.dart' as http;

void getData() async {
    try {
      final String apiEndpoint =
          'https://api.instagram.com/oauth/access_token';
      final Uri url = Uri.parse(apiEndpoint);
      final response = await http.post(url);
      print(response);
    } catch (err) {
      print(err);
    }
}

more brief explanation, What am doing here is parsing the JSON from URL. So in the await http.get(uri) method the uri is the String variable which holds server URL. So to solve this error all we have to do is Wrap the uri or the URL into Uri.parse() method

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdventuneView Question on Stackoverflow
Solution 1 - FlutterjamesdlinView Answer on Stackoverflow
Solution 2 - FlutterTushar MahmudView Answer on Stackoverflow
Solution 3 - FlutterParesh MangukiyaView Answer on Stackoverflow