0

My application uses code to determine Internet access. Here is its code:

//Internet route
class InternetRoute extends StatefulWidget {
  const InternetRoute({Key? key}) : super(key: key);

  @override
  State<InternetRoute> createState() => _InternetRouteState();
}

class _InternetRouteState extends State<InternetRoute> {
  bool ActiveConnection = false;
  String T = "";
  InternetDialogHandler _internetDialogHandler = InternetDialogHandler();
  Future CheckUserConnection() async {
    try {
      final result = await InternetAddress.lookup('example.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        setState(() {
          ActiveConnection = true;
          T = "Turn off the data and repress again";
        });
      }
    } on SocketException catch (_) {
      setState(() {
        ActiveConnection = false;
        T = "Turn On the data and repress again";
        showInternetDialog(context);
        // _internetDialogHandler.showInternetDialog(context);
      });
    }
  }
  @override
  void initState() {
    CheckUserConnection();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("GeeksforGeeks"),
      ),
      body: Column(
        children: [
          Text("Active Connection? $ActiveConnection"),
          const Divider(),
          Text(T),
          OutlinedButton(
              onPressed: () {
                CheckUserConnection();
              },
              child: const Text("Check"))
        ],
      ),
    );
  }
}


//Alert Dialog about Internet connection
showInternetDialog(BuildContext context) {

  // set up the button
  Widget okButton = Center(
    child: TextButton(
    child: Text("OK"),
    onPressed: () {
      Navigator.of(context).pop(); // dismiss dialog
    },
  ),
  );

  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    // title: Text("My title"),
    content: Text("Internet connection required"),
    actions: [
      okButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

This is not my code. You can easily determine that it is taken from open sources. This solution is found quite often on the Internet. And I have never read about the problem with it. Therefore, I am very surprised that this method works differently in the Android Studio emulator, a third-party emulator, and a real Android device.

In particular, when I create an APK file and check for Internet access on a real device and another emulator, I always get the answer, "Internet connection required", that is, my application “thinks” that there is no Internet access.

Why is this happening? In all cases, Internet access is actually there.

What could be wrong in this code? Why do different devices give different results?

Future CheckUserConnection() async {
    try {
      final result = await InternetAddress.lookup('example.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        setState(() {
          ActiveConnection = true;
          T = "Turn off the data and repress again";
        });
      }
    } on SocketException catch (_) {
      setState(() {
        ActiveConnection = false;
        T = "Turn On the data and repress again";
        showInternetDialog(context);
        // _internetDialogHandler.showInternetDialog(context);
      });
    }
  }

Thanks in advance.

1 Answer 1

0

This code is correct. Just need to add uses-permission android:name="android.permission.INTERNET" / in AndroidManifest.xml.

I have also found information that uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" can be added.

But I don't know how correct this is. I checked the first option. Now my application "sees" Internet access. Should I add the second line just in case? I don’t know.

2
  • The code is not correct in two places. First, super.initState() always comes first. Second, initState cannot be async, and thus you may not yet have a value for ActiveConnection (should be lowercase, btw) when you begin build(). You need to fix both of those. Jul 10, 2022 at 16:11
  • Thank you very much for this helpful comment. As I wrote earlier, I have come across this code on the Internet many times, so I had no doubts about its correctness. I am glad that you pointed out these errors.
    – Texter
    Jul 11, 2022 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.