Skip to content

burhanaksendir/universal_internet_checker

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Universal Internet Checker

Listenable class to check internet connectivity in Web and Mobile (not tested on desktop yet)

You can also use this in a StreamProvider to give network awareness to your entire app.

Installation

Include universal_internet_checker in your pubspec.yaml file or add it from pub:

flutter pub add universal_internet_checker

Usage

Default check interval is 5 seconds if last status was "OFFLINE", and 25 seconds if last status was ONLINE

to change:

UniversalInternetChecker._intervalOffline = Duration(milliseconds:5000);
UniversalInternetChecker._intervalOnline = Duration(milliseconds:25000);

To use this package, just import it into your file and call the static method checkInternet as follows:

import 'package:universal_internet_checker/universal_internet_checker.dart';

...

ConnectionStatus status = await UniversalInternetChecker.checkInternet();

...

Note: You can set the static variable "checkAddress" to a specific URL to make the operation and check the internet connection. By default, this value is www.google.com. Do not include (http:// or https://) or any subdirectory in your address

UniversalInternetChecker.checkAddress = 'www.example.com';

Note: You can listen for internet connection changes. Here is the example

import 'package:universal_internet_checker/universal_internet_checker.dart';

...
StreamSubscription? subscription;
ConnectionStatus? _status;

@override
void initState() {
  super.initState();
  subscription = UniversalInternetChecker.onConnectionChange.listen((status) {
    setState(() {
      _status = status;
    });
  });
}

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

...

Note: If you're using provider, use

StreamProvider<ConnectionStatus>(
  create: (context) => UniversalInternetChecker().onConnectionChange,
  initialData: ConnectionStatus.online)

About

Flutter Library that works on all platforms

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 60.7%
  • HTML 34.1%
  • Swift 3.7%
  • Kotlin 1.1%
  • Objective-C 0.4%