0

I am trying to create a simple wrapper for the rest client by facing following issue:

I have an external class:

typedef void Callback(int code, String body);

class HttpClient {
void post(String url, Map<String, String> headers, Map<String, String> params,
   Callback callback) async {
 final encoding = Encoding.getByName('utf-8');

   Response response =
     await post(url, headers: headers, body: params, encoding: encoding);
   callback(200, "lol");
 }
}

My await post(...) is highlighted and says the following error: "The expression here has a type of 'void', and therefore can't be used.

Interesting, that if I change my method to private the highlighting disappears.

I can also declare post as a private function and then call it from another public fuction passing all the same params and it works.

How should I write this function and then call it another place? Thanks!

EDIT

Thanks for @Jack I could find the solution:

import should be

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

And Response response = await post(url, headers: headers, body: params, encoding: encoding);

Should be changed to

 var response =
    await http.post(url, headers: headers, body: params, encoding: encoding);

1 Answer 1

2

The IDE thinks you are referencing your post method instead of http.post as your method returns a void. This can be solved by replacing: await post(...) with await http.post(...). Hope this help! If this doesn't work please leave a comment below.

Sign up to request clarification or add additional context in comments.

4 Comments

@Igor Skryl , if this solved your problem, please accept as an answer. Thanks :)
thanks for the answer! It says "undefined name 'http'"
I am just little bit confused, because when I used the same method in the State with the same imports it worked
Ok, your post helped, but more explanations should be added

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.