The operator '+' isn't defined for the type 'Uri'
509
Use it this way, by combining your strings first, then parsing your Uri. Uri class\object is not a string that you can concatenate strings by using +. Do the following:
String _urlBase = 'http://192.168.1.236/connect/api/v1';
keep the getToken() function as it as, and then use this:
authData(data, apiUrl) async {
try {
String _finalUrl = _urlBase + apiUrl;
Uri _uri = Uri.parse(_finalUrl);
return await http.post(_uri,
body: jsonEncode(data), headers: _setHeaders());
} catch (e) {
print(e);
}
}
That should solve your problem.
Author by
Kevnlan
Updated on December 19, 2022Comments
-
Kevnlan over 1 year
I have a project whereby I am using the http package to make various http requests. I recently updated the package and I cannot append url links to the end of the base url that I have. Here is the sample code
var _url = Uri.parse('url here'); var token; _getToken() async { SharedPreferences localStorage = await SharedPreferences.getInstance(); token = jsonDecode(localStorage.getString('token'))['token']; } authData(data, apiUrl) async { try { var fullUrl = _url + apiUrl; return await http.post(fullUrl, body: jsonEncode(data), headers: _setHeaders()); } catch (e) { print(e); } }
This is the error it displays
The operator '+' isn't defined for the type 'Uri'. Try defining the operator '+'.dartundefined_operator
The error occurs at
var fullUrl = _url + apiUrl;
specifically the + operator. Before updating it worked well however I cannot find the solution to it -
Kevnlan about 3 yearsI dont understand