2

I'm trying to post data to node API from flutter. my code is as follows.

final String uri = "http://localhost:3000";
Map<String, String> userBody = {
"firstName":"Vishal",
"lastName":"Soni",
"email":"[email protected]",
"mobile":"1234567890",
"password":"123",
};

Future<dynamic> postData() async {
try {
  http.Response res = await http.Client().post("$uri/register", body: jsonEncode(userBody), headers:    {
    'Content-Type': 'application/json',
  });
  print(res);
} catch (e) {
  print(e);
 }
}

Getting following error enter image description here

1
  • There's not nearly enough information here to solve your problem -- I'd start with verifying your API is working properly. Commented Oct 20, 2020 at 11:39

2 Answers 2

3

You need to change the localhost. It should be your IP address.

     final String uri = "http://10.0.2.2:3000";

Look here for the same issue on Github.

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

2 Comments

This is only applicable if you're running your app on an emulator, where 10.0.2.2 is looped back to the host machine's address
Yeah I'm running on an emulator
2

run the below command in the terminal

adb reverse tcp:3000 tcp:3000 

Comments

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.