Muhammad Tawil
Muhammad Tawil

Reputation: 167

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

i use simple asynchronous program with one function but i got this error:

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

void main() {
 print(getData());
}

Future<http.Response> getData() {
 return http.get("https://jsonplaceholder.typicode.com/users");
}

can any one help me solve this error ?

Upvotes: 0

Views: 106

Answers (2)

Steve
Steve

Reputation: 943

Since version 0.13.0-nullsafety.0 of http, you should only use an Uri insteand of a string.

[Changelog] (https://pub.dev/packages/http/changelog)

Breaking All APIs which previously allowed a String or Uri to be passed now require a Uri.

So your could should look like this instead:

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

void main() {
  print(getData());
}

Future<http.Response> getData() {
  return http.get(Uri.parse("https://jsonplaceholder.typicode.com/users"));
}

Upvotes: 0

dariowskii
dariowskii

Reputation: 315

From 0.13.0-nullsafety.0 version "All APIs which previously allowed a String or Uri to be passed now require a Uri". (here)

You can follow the README as an example:

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

var url = Uri.parse('https://example.com/whatsit/create');
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');

print(await http.read('https://example.com/foobar.txt'));

Upvotes: 2

Related Questions