我正在尝试将我的 Flutter 应用程序连接到本地计算机上运行的 MongoDB 的 Node.js 后端。在后端,我使用以下 URL:
const MONGO_DB_CONFIG = {
DB: "mongodb://localhost/ecommerce-app"
}
在 Postman 中,我可以使用
http://localhost:4000/api/products/
成功访问后端 API。
但是,当我尝试使用以下 URL 从 Flutter 应用程序进行连接时:
class Config {
static const String apiURL = "10.0.2.2:4000";
}
我在终端上收到以下错误:
Performing hot restart...
Syncing files to device sdk gphone64 x86 64...
Restarted application in 2,077ms.
I/.mongo_database(18019): NativeAlloc concurrent copying
GC freed 56675(1921KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 2514KB/5029KB, paused 544us,73us total 273.631ms
======== 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 = 38912
如何正确配置 Flutter 应用程序以连接到本地计算机上运行的 Node.js 后端?
将您的代码更改为
class Config {
static const String apiURL = "http://10.0.2.2:4000";
}