Home > Software engineering >  how to detect connection in flutter
how to detect connection in flutter

Time:06-29

so i have this function which detect if there is connection or not . if yes the var activeConnection is true else false . So if the connection is working i'm going to call a method sendEmail() which work with the plugin mailer . My problem is when i activated the WIFI it can send the email then if i turn it off an exception is shown

E/flutter ( 5347): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: Failed host lookup: 'smtp.gmail.com' (OS Error: No address associated with hostname, errno = 7)

in this case activeConnection is true but when it try to send the email it can't find the connection . if i turn off the wifi and wait for a moment before i send the email it can detect that there is no wifi so i tried to add a sleep function before it check the connection but i'm facing the same problem . this is the code :

  Future checkUserConnection() async {
    try {
      //sleep(const Duration(seconds: 10));
      final result = await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        setState(() {
          activeConnection = true;
          print(activeConnection);
        });
      }
    } on SocketException catch (_) {
      setState(() {
        activeConnection = false;
        print(activeConnection);
      });
    }
    //print(activeConnection);
  }

and this is where i call my function

          onTap: () {
            checkUserConnection();
            print(activeConnection);
     
            if (activeConnection) {
              sendEmail();
              ScaffoldMessenger.of(context).showSnackBar(showSnackBar(
                  false, "email sended ", Icons.error_outline));
            } else {
              ScaffoldMessenger.of(context).showSnackBar(showSnackBar(
                  true,
                  "check your internet connection !!!",
                  Icons.error_outline));
            }
            print("hello");
          },

CodePudding user response:

You have to make async function like this:

Future<bool> checkUserConnection() async {
         try {
           final result = await InternetAddress.lookup('google.com');
           if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
              return true;
         }
        } on SocketException catch (_) {
            return false;
        }

     }

After that onTap() you have to code like this:

onTap: () {
        checkUserConnection().then((activeConnection){
           if (activeConnection) {
              sendEmail();
              ScaffoldMessenger.of(context).showSnackBar(showSnackBar(
                false, "email sended ", Icons.error_outline));
          } else {
              ScaffoldMessenger.of(context).showSnackBar(showSnackBar(
              true, "check your internet connection !!!",
              Icons.error_outline));
          }
       });
        
      }
  • Related