How to check Internet status and show message in flutter?

The Internet is the basic need for any App to work. When a user opens an App, it is better practice to find if the internet is active or not and inform the user if there is no internet connection. There are many ways the user can be notified which changes from App to App. Below are few ways which can be used,

  • Through a simple push notification.
  • Through an alert or a dialog.
  • Through an independent page with some custom good looking design.

Additionally, the App should be able to resume if the change in network connection state starts working.

You have to use the ‘Connectivity Flutter Package’ to achieve this feature on your App. This package helps to know either your device is online or offline. You have to subscribe StreamSubscription returned by this package to show the internet connection message automatically like below.

Step 1: Add the connectivity plus plugin inside the pubspec.yamlfile

Now, see the example below, and apply exactly same method to show internet connection offline message automatically in your app layout.

full-code example:

import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class CheckConnection extends StatefulWidget{
  State createState() {
    return _CheckConnection();

class _CheckConnection extends State{
  StreamSubscription? internetconnection;
  bool isoffline = false;
  //set variable for Connectivity subscription listiner
  void initState() {
    internetconnection = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
        // whenevery connection status is changed.
        if(result == ConnectivityResult.none){
             //there is no any connection
             setState(() {
                 isoffline = true;
        }else if(result =={
             //connection is mobile data network
             setState(() {
                isoffline = false;
        }else if(result == ConnectivityResult.wifi){
            //connection is from wifi
            setState(() {
               isoffline = false;
    }); // using this listiner, you can get the medium of connection as well.


  dispose() {
    //cancel internent connection subscription after you are done

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title:Text("Flutterfever Connection")),
             child: Column(children: [
                     child: errmsg("No Internet Connection Available", isoffline),
                     //to show internet connection message on isoffline = true.

                     //this is your content
                      margin: EdgeInsets.all(30),
                      child: Center( 
                         child:Text("Check Connections for",


  Widget errmsg(String text,bool show){
  //error message widget.
      if(show == true){
        //if error is true then show error message box
        return Container(
            padding: EdgeInsets.all(10.00),
            margin: EdgeInsets.only(bottom: 10.00),
            child: Row(children: [

                    margin: EdgeInsets.only(right:6.00),
                    child: Icon(, color: Colors.white),
                ), // icon for error message
                Text(text, style: TextStyle(color: Colors.white)),
                //show error message text
         return Container();
         //if error is false, return empty container.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More