Code Monkey home page Code Monkey logo

outdatedguy / internet_connection_checker_plus Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 12.0 464 KB

A pure Dart library that checks for internet by opening a socket to a list of specified addresses, each with individual port and timeout.

Home Page: https://pub.dev/packages/internet_connection_checker_plus

License: BSD 3-Clause "New" or "Revised" License

Dart 39.67% Kotlin 0.19% Swift 2.18% Objective-C 0.04% CMake 22.98% C++ 28.29% C 1.68% HTML 1.67% Ruby 3.30%
flutter flutter-package internet-connection-checker

internet_connection_checker_plus's Introduction

Internet Connection Checker Plus

A Flutter package to check your internet connection with subsecond response times, even on mobile networks!

pub package GitHub

GitHub issues GitHub issues closed


This library provides functionality to monitor and verify internet connectivity by checking reachability to various Uris. It relies on the connectivity_plus package for listening to connectivity changes and the http package for making network requests.

Features

  • Check internet connectivity status
  • Listen for internet connectivity changes

Supported Platforms

Platform Check Connectivity Listen for Changes
Android
iOS
macOS
Linux
Windows
Web

Permissions

Android

Add the following permissions to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />

macOS

Add the following permissions to your macOS .entitlements files:

<key>com.apple.security.network.client</key>
<true/>

For more information, see the Flutter Networking Documentation.

Usage

1. Add dependency

Add the internet_connection_checker_plus package to your pubspec.yaml file:

dependencies:
  internet_connection_checker_plus: ^2.4.1

2. Import the package

Import the internet_connection_checker_plus package into your Dart file:

import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';

3. Checking for internet connectivity

The simplest way to check for internet connectivity is to use the InternetConnection class:

bool result = await InternetConnection().hasInternetAccess;

4. Listening for internet connectivity changes

The InternetConnection class also provides a stream of InternetStatus that can be used to listen for changes in internet connectivity:

final listener = InternetConnection().onStatusChange.listen((InternetStatus status) {
  switch (status) {
    case InternetStatus.connected:
      // The internet is now connected
      break;
    case InternetStatus.disconnected:
      // The internet is now disconnected
      break;
  }
});

Don't forget to cancel the subscription when it is no longer needed. This will prevent memory leaks and free up resources:

listener.cancel();

5. Add custom Uris to check

The InternetConnection class can be configured to check custom Uris for internet connectivity:

final connection = InternetConnection.createInstance(
  customCheckOptions: [
    InternetCheckOption(uri: Uri.parse('https://example.com')),
  ],
);

Note

Make sure the custom Uris have no caching enabled. Otherwise, the results may be inaccurate.

Note

On web platform, make sure the custom Uris are not CORS blocked. Otherwise, the results may be inaccurate.

6. Add custom success criteria

The InternetConnection class can be configured to check custom Uris for internet connectivity using custom success criteria:

final connection = InternetConnection.createInstance(
  customCheckOptions: [
    InternetCheckOption(
      uri: Uri.parse('https://example.com'),
      responseStatusFn: (response) {
        return response.statusCode >= 69 && response.statusCode < 169;
      },
    ),
    InternetCheckOption(
      uri: Uri.parse('https://example2.com'),
      responseStatusFn: (response) {
        return response.statusCode >= 420 && response.statusCode < 1412;
      },
    ),
  ],
);

Default Uris

The InternetConnection class uses the following Uris by default:

URI Description
https://icanhazip.com Response time is less than 100ms, CORS enabled, no-cache
https://jsonplaceholder.typicode.com/posts/1 Response time is less than 100ms, CORS enabled, no-cache
https://pokeapi.co/api/v2/pokemon/1 Response time is less than 100ms, CORS enabled, no-cache
https://reqres.in/api/users/1 Response time is less than 100ms, CORS enabled, no-cache

Some Tested URIs

URI Description
https://ifconfig.me/ip Payload is less than 50 bytes, CORS enabled, no-cache
https://ipecho.net/plain Payload is less than 50 bytes, CORS enabled, no-cache
https://lenta.ru Russia supported, CORS enabled, no-cache
https://www.gazeta.ru Russia supported, CORS enabled, no-cache
https://ipapi.co/ip CORS enabled, no-cache
https://api.adviceslip.com/advice CORS enabled, no-cache
https://api.bitbucket.org/2.0/repositories CORS enabled, no-cache
https://www.boredapi.com/api/activity CORS enabled, no-cache
https://api.thecatapi.com/v1/images/search CORS enabled, no-cache
https://api.coindesk.com/v1/bpi/currentprice.json CORS enabled, no-cache

Credits

This package is a cloned and modified version of the internet_connection_checker package which is a cloned and modified version of the data_connection_checker package which is no longer maintained.

The aim of this package is to support the web platform which is currently not supported by the internet_connection_checker package.

internet_connection_checker_plus's People

Contributors

outdatedguy avatar tatashidayat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

internet_connection_checker_plus's Issues

address change documentation

Thanks for this one. I need to change the default addresses, for some work places block the url's. Do you have a better way to explain how to change the default addresses. I am a blind guy and not sure if I am missing something that the screen reader is not reading. But can not figure out how to change it. Could you please give me a better example? my email [email protected]. Thank you

[Feature Request] Stop listenning for internet status when app goes to background

Apps are more likely to be killed by the OS if they keep using the network when they are in the background.

Using AppLifecycleListener can we stop the Timer that checks the network status when app is not in the foreground?

My app has no use for information about internet status when its not being actively used. I actually turn off all network services, such as Firebase document listeners, when app goes out of focus.

Thank you!

Continuous logging and unhandled exception

In your package, I tried to console log to check if the status code is equals to 200, and the result is that it's continuous log the print function.
In my use case, I have integrated your package to monitor changes in internet status, triggering UI components such as dialogs and snack bars through the whole app by using navigator context. But the problem is that the code won't stop after timeout. In that case, it would lead to an exception: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4516 pos 12: '!_debugLocked': is not true.
Am I missing something while using your package?

SocketException with flutter 3.13.x

In my Android project using plugin v2.0.0, this error came up when debugging the app on VSCode (v1.18.1), with Flutter 3.13.0 & 3.13.1, when internet is offline.

Exception has occurred.
SocketException (SocketException: Failed host lookup: 'icanhazip.com' (OS Error: The requested name is valid, but no data of the requested type was found.
, errno = 11004))

Output of flutter doctor:

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.13.1, on Microsoft Windows [Version 10.0.22621.2134], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.16)
[√] Android Studio (version 2021.2)
[√] VS Code, 64-bit edition (version 1.81.1)
[√] Connected device (4 available)
[√] Network resources
• No issues found!

No issue with Flutter 3.10.5.

Please advice. Thanks.

Updates

Hi Thank you for this GREAT package.
It works great on iOS simulator, unlike Connectivity_plus package which doesn't work properly on it.

Could you please update it to Dart 3, and maintain this package in the future please.
I've been using your package for my projects for the past 6months and will intend to continue doing so.

Kind regards

Chris

unable to use v2.0.0 with firebase storage plugin

Hi, I am trying to use your latest plugin version 2.0.0 in my project that access firebase storage. However, pub get gave me some error:

[internet_plus] flutter pub get
Resolving dependencies...
Because no versions of internet_connection_checker_plus match >2.0.0 <3.0.0 and internet_connection_checker_plus 2.0.0 depends on http ^1.0.0, internet_connection_checker_plus ^2.0.0 requires http ^1.0.0.
And because firebase_storage >=11.2.3 depends on firebase_storage_web ^3.6.3 which depends on http ^0.13.0, internet_connection_checker_plus ^2.0.0 is incompatible with firebase_storage >=11.2.3.
So, because internet_plus depends on both firebase_storage ^11.2.3 and internet_connection_checker_plus ^2.0.0, version solving failed.
exit code 1

Output of flutter doctor:

$ puro flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.10.5, on Microsoft Windows [Version 10.0.22621.1848], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Build Tools 2019 16.11.16)
[√] Android Studio (version 2021.2)
[√] VS Code, 64-bit edition (version 1.79.1)
[√] Connected device (4 available)
[√] Network resources
• No issues found!

Any thought on how to get around this problem?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.