Flutter : Handling Error SocketException: Connection failed (OS Error: Network is unreachable, errno = 101)
5,749
Make sure you are online, you are connected to internet whether it is mobile or emulator
Make sure you have given internet permission in your app's
android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Comments
-
Zeffry Reynando over 1 year
I have a problem handling the error
SocketException: Connection failed (OS Error: Network is unreachable, errno = 101) address = 192.168.43.159, port = 80
.What I already did :
Setting Android Permission Internet.
My Reference Making Network http error SocketException: Failed host lookup
<uses-permission android:name="android.permission.INTERNET"/>
Make function with
try catch
to handling this errorIt's API function
Future loginMahasiswa(body) async { try { var apiRespon = await http.post('$baseURL/mahasiswa/login', body: body, headers: CrudComponent.headers); int statusCode = apiRespon.statusCode; if (statusCode == 200) { final apiResponJson = json.decode(apiRespon.body); print('Success Load Data Json ($apiResponJson)'); return apiResponJson; } else { final apiResponJson = json.decode(apiRespon.body); print('fail Load Data Json $apiResponJson'); return apiResponJson; } } catch (e) { print(e); return null; } }
It's Button Function
void _loginMahasiswa() async { SharedPreferences preferences = await SharedPreferences.getInstance(); try { final body = {"email": _txtEmail.text, "password": _txtPassword.text}; var loginMahasiswa = await api.loginMahasiswa(body); String message = loginMahasiswa['message']; var dataUser = loginMahasiswa['data']; // String idUser = dataUser[0]['id_user'] ?? "null"; if (loginMahasiswa['status'] == true) { setState(() { preferences.setString('token', dataUser[0]['username']); Navigator.of(context).pushAndRemoveUntil( PageTransition( type: PageTransitionType.rightToLeftWithFade, child: HomePage()), (Route<dynamic> route) => false); }); } else { print(message); _showSnackBar(context, message, Colors.red); } } on SocketException catch (_) { //throw ("No connection Internet"); _showSnackBar(context, 'no connection', Colors.orange); } }
But my
Snackbar
does not show an errorno connection
if I click the button.I'm using a real device to run my app and I purposely turn off the hotspot to test my app not connected to the server.
-
Boby almost 5 yearscan you ping it via cmd or terminal?
-
Zeffry Reynando almost 5 years@MyNameIs what should i ping ?
-
Boby almost 5 yearsping the ipaddress
-
Swift almost 5 yearsFrom what I see its
NoSuchMethodError
exception that you are not handling -
Zeffry Reynando almost 5 years@SwiftingDuster Sorry ? So i'm not handling the
SocketException
? -
Zeffry Reynando almost 5 years@MyNameIs My result ping ,
Ping statistics for 192.168.43.159: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms
-
Aziz almost 5 years@ZeffryReynando this means your url is up and running
-