Search code examples
node.jsflutterdartimage-upload

How to resolve "Connection refused (OS Error: Connection refused, errno = 111)" in Flutter app?


I'm encountering a "Connection refused" error in my Flutter app when uploading an image to a Node.js backend. The terminal and app errors are as follows:

Terminal Errors:

> Performing hot restart... Syncing files to device sdk gphone64 x86
> 64... Restarted application in 1,691ms. D/CompatibilityChangeReporter( 9724): Compat change id reported: 78294732; UID 10162; state: DISABLED
> D/MediaScannerConnection( 9724): Scanned
> /data/user/0/com.example.mongo_backend/cache/2fdd677e-2569-4d9c-8349-b461215be87f8625165904540994379.jpg
> to null
> 
> ======== Exception caught by image resource service ================================================ The following SocketException was thrown resolving an image codec: Connection
> refused (OS Error: Connection refused, errno = 111), address =
> localhost, port = 46640
> 
> When the exception was thrown, this was the stack: 
> #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:682:35)
> #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1817:26)
> #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
> #3      Socket._startConnect (dart:io-patch/socket_patch.dart:2038:22)
> #4      Socket.startConnect (dart:io/socket.dart:759:21)
> #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2453:20)

What does it mean?

Also, I have this error on flutter app, I tried to upload an image with the back end node.js, but doesn't work:

Flutter App Error:

enter image description here

What I've Tried:

  • I've checked that my Node.js backend server is running and listening on the specified port (46640 in this case).
  • I've verified that there are no firewall rules blocking the connection.

Specific Question:

How can I troubleshoot this "Connection refused" error and successfully upload images from my Flutter app to the Node.js backend?


Solution

  • Use your machine IP instead of "localhost" to access your API (something like 192.168.x.x). Your flutter app is recognizing "localhost" as your emulator instead of your PC (where is your API running), so there are two different localhosts.