[Solved] The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’

Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’ in a flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’ Error Occurs ?

I have implemented simple API call with http but I am facing URI error. As just like below.

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

Here is my code

await http.get(Uri.encodeFull("https://jsonplaceholder.typicode.com/todos/"), body: {
      "id": "1",
    });

How to Solve The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’ Error?

  1. How to Solve The argument type 'String' can't be assigned to the parameter type 'Uri' Error?

    To Solve The argument type 'String' can't be assigned to the parameter type 'Uri' Error you just have to parse your string to URI formate. Actually package:http that made all functions that previously accepted Uris or Strings now accept only Uris instead. You have to use Uri.parse to create Uris from Strings.

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

    To Solve The argument type 'String' can't be assigned to the parameter type 'Uri' Error you just have to parse your string to URI formate. Actually package:http that made all functions that previously accepted Uris or Strings now accept only Uris instead. You have to use Uri.parse to create Uris from Strings.

Solution 1: use Uri.parse

Actually package:http that made all functions that previously accepted Uris or Strings now accept only Uris instead. You have to use Uri.parse to create Uris from Strings.

Old Method Of http packageNew Method Of http package
http.get(StringURL)http.get(Uri.parse(StringURL))
http.post(StringURL)http.post(Uri.parse(StringURL))

So that Uri.parse will convert your string to URI format and that is acceptable by http package so your error is solved.

Solution 2: use Uri.https

There is second method to parse string to Uri. just take look below.

String url ='https://jsonplaceholder.typicode.com/todos/1';

int todos=2;


http.get(Uri.https(url,'/$todos'),
  headers:{Content-type':'application/json',
  },
 );

If you want to get data from HTTP instead of HTTPS Then you just have to change Uri.http(), and that’s all.

Summery

So, It’s All About This Error. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.

Also Check Out Below Tutorials

Leave a Comment

  Canva Pro Crack Filmora Pro Crack Spotify Premium Free Download Tradingview Premium Free