http Dart package examples are outdated, how to use client.get()


Here’s the second example given by the http package:

var client = http.Client();
try {
  var uriResponse = await'',
      body: {'name': 'doodle', 'color': 'blue'});
  print(await client.get(uriResponse.bodyFields['uri']));
} finally {

I get error: uriResponse.bodyFields['uri'] no such method.

I can see that there’s no property named bodyFields in the class Response:

So how should I use the client.get()?

I checked the docs for the function and I wrote this:

print(await client.get(uriResponse.request.url, {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',

But this also fails with error:

Class 'IOClient' has no instance method 'get' with matching arguments.
E/flutter (19613): Receiver: Instance of 'IOClient'
E/flutter (19613): Tried calling: get(Instance of '_SimpleUri', Instance of '_CompactLinkedHashSet<Map<String, String>>')
E/flutter (19613): Found: get(dynamic, {Map<String, String> headers}) => Future<Response>


write your post as below and print the response separate.

final response = await
    '*endPoint url*',
    headers: *headers if you have any*,
    body: jsonEncode({'name': 'doodle', 'color': 'blue'}


and follow the same structure for the get method as well.

Answered By – Sajith

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *