GithubHelp home page GithubHelp logo

anthochamp / flutter-connectivity Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 296 KB

Stateful connectivity_plus extended with Internet connectivity

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

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

Dart 20.71% Kotlin 0.21% Swift 2.19% Objective-C 0.07% CMake 32.10% C++ 39.14% C 2.44% HTML 3.15%
connectivity-plus flutter internet-connectivity

flutter-connectivity's Introduction

connectivity_plus extended with Internet Connectivity

This package extends the connectivity_plus plugin :

  • with Internet Connectivity state (accessible on-demand or via a change stream),
  • with a fix that refresh connectivity when an Android app resumes from background.
  • with a stateful state of both the connectivity_plus and Internet connectivity states (with some limitations, see Important note on Internet Connectivity Stream below).

Usage

Access to the original connectivity_plus members

checkConnectivity

final ConnectivityPlusState state = await Connectivity().checkConnectivityPlusState();

print(state); // ConnectivityPlusState.none, .mobile, .ethernet, etc.

onConnectivityChanged stream

Connectivity().getConnectivityPlusStream().listen((state) {
  // It is guaranteed that two successive events have different values.
  print(result);
});

Internet connectivity

For Internet connectivity, this package uses ac_inet_connectivity_checker with the recommended endpoints configuration (randomized root nameservers' IPv4 and IPv6 addresses).

final state = Connectivity().lastInetConnectivityState;

if (state == InetConnectivityState.disconnected) {
  // disconnected (same as ConnectivityResult.none)
} else if (state == InetConnectivityState.connected) {
  // connected to a network without Internet access.
} else if (state == InetConnectivityState.internet) {
  // connected to Internet.
}

On-demand fresh value

Contrary to the network connectivity test, Internet connectivity test allows the definition of a timeout. It can be avoided if you want to handle the timeout yourself with the cancelable operation. Be aware that if you don't specify a timeout, and in some condition, it might uses the default operating system timeout which is usually 120s.

For Flutter on the Web, it will not make any network request but instead deduce the state based on a fresh connectivity_plus state.

final cancelableOperation = Connectivity().checkInetConnectivityState(
  timeout: const Duration(seconds: 3),
);

final state = await cancelableOperation.value;

Stream

โš  Important: In some condition it won't automatically detect the transition from InetConnectivityState.internet to InetConnectivityState.connected. Please check "Important note on Internet Connectivity Stream" below.

Connectivity().listen((state) {
  // It is guaranteed that two successive events have different values.
  print(state);
});

Important note on Internet Connectivity Stream

Detecting Internet connectivity changes without a constant background test is a tricky business.

Detecting Internet access

This package starts a background test (ONLY) when there's no Internet connectivity, so it can guarantee that when the app gets back online, you'll be quickly informed of it.

The speed at which you'll be informed of it depends on the network configuration :

  • If the device changes network configuration (going from a 4G network to a Wifi network for example), Internet detection will be almost instantaneous.
  • Whereas if the device is connected to Internet via a router (eg. connected to Wifi), but the Wifi network itself has no Internet access, it might take as much time as the duration you've specified in the backgroundChecksInterval configuration (which is 0 by default).

Detecting loss of Internet access

The real issue is to detect loss of connection when there's no network configuration changes. So for example if the device is connected to a Wifi network with its network router which looses access to the Internet, the only way to detect that the app lost Internet is either to make constant Internet Connectivity checks, or to check for error when the app makes network request (to the Internet, obviously).

The package make the choice to not do that constant background Internet check itself when it thinks it has access to the Internet (when Connectivity().lastInetConnectivityState == InetConnectivityState.internet). Instead it provides a notifyChange that you must call if you think the Internet connectivity has changed (on a HTTP request error for example). You can call it even if you're unsure.

import 'package:http/http.dart' as http;

try {
  var response = await http.get(Uri.https('google.com'));
} on SocketException {
  Connectivity().notifyChange();

  rethrow;
} catch(error) {
  print(error);
}

On the other hand, if you really want to implement the constant background Internet check, here's an example:

CancelableTimer? connectivityChecker;

Connectivity().listen((state) {
  if (state == InetConnectivityState.internet) {
    connectivityChecker = CancelableTimer.periodic(
      const Duration(seconds: 60),
      (_) => Connectivity().checkInetConnectivityState(),
      wait: true,
    );
  } else {
    connectivityChecker?.cancel();
    connectivityChecker = null;
  }
})

flutter-connectivity's People

Contributors

anthochamp avatar grenadine-renovate-bot avatar

Watchers

 avatar

Forkers

prasant10050

flutter-connectivity's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

Renovate tried to run on this repository, but found these problems.

  • WARN: Package lookup failures

Warning

Renovate failed to look up the following dependencies: Failed to look up maven package dev.flutter.flutter-plugin-loader:dev.flutter.flutter-plugin-loader.gradle.plugin.

Files affected: example/android/settings.gradle


Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/flutter.yml
  • actions/checkout v4
  • subosito/flutter-action v2
  • actions/checkout v4
  • subosito/flutter-action v2
gradle
example/android/gradle.properties
example/android/settings.gradle
  • dev.flutter.flutter-plugin-loader 1.0.0
  • com.android.application 8.6.0
  • org.jetbrains.kotlin.android 2.0.20
example/android/build.gradle
example/android/app/build.gradle
gradle-wrapper
example/android/gradle/wrapper/gradle-wrapper.properties
  • gradle 8.10
pub
example/pubspec.yaml
  • flutter
  • cupertino_icons ^1.0.2
  • flutter_lints ^4.0.0
  • dart >=2.19.6 <4.0.0
pubspec.yaml
  • ac_dart_essentials ^0.2.1
  • ac_inet_connectivity_checker ^0.1.4
  • ac_flutter_essentials ^0.1.0
  • async ^2.10.0
  • connectivity_plus >=6.0.0
  • flutter
  • ac_lints ^0.3.0
  • build_runner ^2.3.3
  • import_sorter ^4.6.0
  • dart >=2.19.5 <4.0.0

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.