将图像上传到 Node.js 后端时,我的 Flutter 应用程序遇到“连接被拒绝”错误。终端和app错误如下:
终端错误:
> 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)
这是什么意思?
另外,我在 flutter 应用程序上遇到此错误,我尝试使用后端 -node.js- 上传图像,但不起作用:
Flutter 应用程序错误:
我尝试过的:
具体问题:
如何解决“连接被拒绝”错误并成功将图像从 Flutter 应用上传到 Node.js 后端?
使用您的机器 IP 而不是“localhost”来访问您的 API(例如 192.168.x.x)。您的 flutter 应用程序将“localhost”识别为您的模拟器而不是您的 PC(您的 API 在哪里运行),因此有两个不同的本地主机。