In this post, we are going to show you how to solve "Unhandled Exception: DioError [DioErrorType.other]: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 51746" Error in Flutter App.
E/flutter (16776): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)]
Unhandled Exception: DioError [DioErrorType.other]:
SocketException: OS Error: Connection refused, errno = 111,
address = localhost, port = 51746
E/flutter (16776): Source stack:
E/flutter (16776): #0 DioMixin.fetch (package:dio/src/dio_mixin.dart:473:35)
E/flutter (16776): #1 DioMixin.request (package:dio/src/dio_mixin.dart:468:12)
E/flutter (16776): #2 DioMixin.get (package:dio/src/dio_mixin.dart:55:12)
Here, you can clearly see "address = localhost", which means you are accessing the URL on localhost, but you are getting the error. See the examples below to solve this issue.
"localhost" is not identified domain name or has no DNS record, therefore, your emulator is not getting the actual IP address to the localhost server from the Mobile app or from Emulator, for example, let's try to access the address from "Chrome" browser on Mobile/Emulator:
To solve this error, use a local IP address instead of "localhost" in your Flutter/Dart code, and also be careful if WAMP or XAMPP is running? For example:
Response response = await dio.get("http://localhost/test/data.php");
//Connection refused error,
Change this to:
Response response = await dio.get("http://192.168.0.235/test/data.php");
//use local IP or actual live URL
Here, We have used the local IP address of the local server, or you can also use the Live URL if API is hosted in the live server.
And also be careful, here you are using "HTTP" instead of secure "HTTPS" protocol, you may get the error, see this link: How to Solve ’Insecure HTTP is not allowed by platform’ Error
For Windows User:
ipconfig
//output: IPv4 Address. . . . . . . . . . . : 192.168.0.235
For Linux User:
ip a
for Mac OS Users:
ipconfig getifaddr en1
//for wired connection
ipconfig getifaddr en0
//for wifi connection
In this way, you can solve the "Unhandled Exception: DioError [DioErrorType.other]: SocketException: OS Error: Connection refused" error for Localhost address in your Flutter App.
Please Wait...
1 Commet on this Article
nitt
trimakasih flutter campus, cukup sederhana tapi sangat bermakna