title: Flutter : Handling Error SocketException: Connection failed (OS Error: Network is unreachable, errno = 101)
body: <p><a href="https://i.stack.imgur.com/WXgjF.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WXgjF.gif" alt="No Connection"></a>
  I have a problem handling the error <code>SocketException: Connection failed (OS Error: Network is unreachable, errno = 101)  address = 192.168.43.159, port = 80</code>.
  <p> What I already did : <p></p>
  
  <h3>Setting Android Permission Internet.</h3>
  
  <p>My Reference <a href="https://github.com/flutter/flutter/issues/27883" rel="nofollow noreferrer">Making Network http error SocketException: Failed host lookup</a></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>
  
  <h1>Make function with <code>try catch</code> to handling this error</h1>
  
  <h3>It's API function</h3>
  
  <pre><code>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;
      }
    }
  </code></pre>
  
  <h3>It's Button Function</h3>
  
  <pre><code>  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&lt;dynamic&gt; route) =&gt; false);
          });
        } else {
          print(message);
          _showSnackBar(context, message, Colors.red);
        }
      } on SocketException catch (_) {
        //throw ("No connection Internet");
         _showSnackBar(context, 'no connection', Colors.orange);
      }
    }
  </code></pre>
  
  <p>But my <code>Snackbar</code> does not show an error <code>no connection</code> if I click the button.</p>
  
  <blockquote>
    <p>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.
    <a href="https://i.stack.imgur.com/x48lW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x48lW.png" alt="No Connection Image"></a></p>
  </blockquote>