Check an internet connection available on Flutter app

2.8k Views Asked by At

I am looking for a way to check an internet connection from my flutter app. I tried to follow processes below.

  1. use lookup final result = await InternetAddress.lookup('example.com') and check result.isNotEmpty && result[0].rawAdress.isNotEmpty

  2. use internet_connection_checker plugin final result = await InternetConnectionChecker().hasConnection

My test device(real device) is connected to wifi, But need to be logged in to allow using it (but not logged in at that time). It appear that the result always return true even if the app can't use the internet.

additional information

When I try to use the Internet through the address (1.1.1.1, google.com, example.com, etc.) in my device's browser It always redirects to the login page. I think this might be a problem why internet status check always return true.

Does anyone know what I might have done wrong? or what do I have to do to get what I want?

Thank you

1

There are 1 best solutions below

1
Sibi Kumar On
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:async';
// import 'dart:io';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  ////////////////// CHECK CONNECTIVITY CONTINUOUSLY ///////////////////////
  // Define Variables
  StreamSubscription connectivitySubscription;
  ConnectivityResult previousresult;
  @override
  void initState() {
    super.initState();
    connectivitySubscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult nowresult) {
      if (nowresult == ConnectivityResult.none) {
        // print('Not Connected');
        //TODO Flutter Toaster for None
        Fluttertoast.showToast(
          msg: "Network Connection Error",
          backgroundColor: Colors.black,
          textColor: Colors.white,
          fontSize: 16.0,
        );
      }
      // when mobile and wifi network connected
      else if (previousresult == ConnectivityResult.none) {
        // print('Connected');
        if (nowresult == ConnectivityResult.mobile) {
          // print('Mobile Network Connected');
          //TODO Flutter Toaster for Mobile
          Fluttertoast.showToast(
            msg: "Mobile Network Connected",
            backgroundColor: Colors.black,
            textColor: Colors.white,
            fontSize: 16.0,
          );
        } else if (nowresult == ConnectivityResult.wifi) {
          // print('WiFi Network Connected');
          //TODO Flutter Toaster for WiFi
          Fluttertoast.showToast(
            msg: "WiFi Network Connected",
            backgroundColor: Colors.black,
            textColor: Colors.white,
            fontSize: 16.0,
          );
        }
      }
      previousresult = nowresult;
    });
  }

  @override
  void dispose() {
    super.dispose();
    connectivitySubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.cyan,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Connectivity Status',
              style: TextStyle(
                fontSize: 25.0,
              ),
            ),
          ],
        ),
      ),
    );
  }
}