2

function that gets triggered after button click in flutter. whenever i click the button, theres a connection error and nothing on the server side.

`void getTest() async {
  BaseOptions options = BaseOptions(
    baseUrl: "http://10.0.2.2:3000",
    // connectTimeout: 1000,
    // receiveTimeout: 3000,
  );
  Dio dio = Dio(options);
  try {
    Response resp = await dio.post(
      options.baseUrl + "/test",
    );
  } catch (e) {
    print("Exception: $e");
  }
}`

code for node server. im running node server on localhost:3000

`const express = require('express');
const { Server } = require('ws');

const app = express();

app.get("/test", function(req, res){
  
  console.log(req)
});

app.post("/test", function(req, res){
  
  console.log(req)
});

app.listen(3000, function() {
  console.log("Server started on port 3000");
});
`

debug console shows this error

`Exception: DioError [DioErrorType.connectTimeout]: Connecting timed out [0ms]`
  • 1
    If you're running the project on an emulator, ```localhost``` refers to the emulator's local host and not your pc. Instead of localhost you should use the IP address. This might help for that: https://stackoverflow.com/q/5528850/2315974 – danypata Feb 05 '23 at 14:58

0 Answers0