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?
- 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
Uri
s orString
s now accept onlyUri
s instead. You have to useUri.parse
to createUri
s fromString
s. - 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
Uri
s orString
s now accept onlyUri
s instead. You have to useUri.parse
to createUri
s fromString
s.
Solution 1: use Uri.parse
Actually package:http that made all functions that previously accepted Uri
s or String
s now accept only Uri
s instead. You have to use Uri.parse
to create Uri
s from String
s.
Old Method Of http package | New 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
- Could not determine the dependencies of task ‘:app:compileDebugJavaWithJavac’ flutter
- How to convert int variable to string in Flutter
- error running pod install in flutter on mac
- Flutter iOS build failed Warning: Podfile is out of date
- Undefined class ‘FirebaseUser’