/check_network

Primary LanguageDartBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Pub Star on Github

check_network

Checking Internet availability by pinging DNS servers to check if the Internet connection is available or not.

Table of contents

General info

This package is simply ping DNS sever to check if availability of internet is valid one or not. It is useful because by wrapping MaterialApp widget, with InternetStatusProvider will give access to Stream of internet status which then can be used to perform any action based on internet status. Such as:

Screenrecording_20220828_210854.mp4
Screenrecording_20220828_175602.mp4

Setup

To use this package, you need to install it first. You can install it by running this command:

flutter pub add check_network

Or you can add this to your pubspec.yaml file:

dependencies:

 check_network: ^0.0.1

Features

List of features ready and TODOs for future development

  • Check internet availability
  • Add option to check availability of specific domain address

To-do list:

  • Support internet speed test

How to use

To see full example, please check the example folder.

  1. First, you import the package to your dart file.
import 'package:check_network/check_network.dart';
  1. Warp the Top Most Widget with the InternetStatusProvider. This will provide the Stream of InternetStatus to all children widgets.
InternetStatusProvider(
 currentInternetStatus: CurrentInternetStatus(showConnectedStatusFor: 5),
 child: const MyApp(),
),

Now we will see how to rebuild the widget when internet status changes.

Rebuild Widget

Have a Scaffold with StreamBuilder pass InternetStatusProvider.of(context).onStatusChange to stream parameter that will give access to Internet Status. And, now simple pass a function that return a widget based on the current internet status which will get recalled when internet status changes.

 return Scaffold(
      body: StreamBuilder<InternetStatus>(
        stream: InternetStatusProvider.of(context).onStatusChange,
        builder: (context, snapshot) {
          return Center(child: internetStatusWidgetBuilder(snapshot.data));
        },
      ),
    );

And, that's it we are done. Now, every time the internet status changes, the widget will be rebuilt. Please see the example in the example for full code and many more examples.

Acknowledgements

NOTE: This package is a continuation and took inspiration from data_connection_checker.

I would like to appreciate

  • Dart team for creating such a great language.

  • Flutter team for creating such a great framework.

  • Very Good Ventures team for creating such a great open source project and packages.

  • Flutter Community team for amazing packages and resources.