internet_status 1.0.0
internet_status: ^1.0.0 copied to clipboard
Checks whether the device really has internet access (DNS, socket and HTTP), streams the status, and shows an animated offline banner.
internet_status #
Finds out whether the device can really reach the internet, not just whether it is joined to a network. A Wi-Fi network with no route out counts as offline.
It checks three things: a DNS lookup, a socket connection and an HTTP request. You get the result once, or as a stream that updates when the connection changes, and an animated banner you can show when the device goes offline.
This code was part of extensionresoft 1.x. It is its own package so that apps
that do not need it do not pull in connectivity_plus and rxdart.
Use it #
import 'package:internet_status/internet_status.dart';
final checker = InternetConnectionChecker();
// once
final online = await checker.isInternetConnected;
// as it changes
checker.onIsInternetConnected.listen((online) {
// show or hide your own UI
});
// the details: which checks passed, why one failed, and the connection type
checker.onInternetConnectivityChanged.listen((result) {
print(result.hasInternetAccess);
print(result.connectionType); // ConnectionType.wifi, mobile, ...
print(result.failureReason);
});
InternetResult.hasInternetAccess is true only when the DNS, socket and HTTP
checks all pass.
Banner #
ConnectionBanner is a slide-in banner you place in an Overlay or a Stack.
Its look is set with ConnectionBannerStyle (colours, icon, height, radius,
animation time).
ConnectionBanner(
message: 'No internet connection',
style: const ConnectionBannerStyle(),
onClose: () {/* remove it */},
)
Migrating from extensionresoft 1.x #
extensionresoft 2.0.0 no longer contains these classes. If your app used
InternetConnectionChecker, InternetResult, ConnectionBanner or
ConnectionBannerStyle:
-
Add the package:
dependencies: extensionresoft: ^2.0.0 internet_status: ^1.0.0 -
Change the import in every file that uses them:
// before import 'package:extensionresoft/extensionresoft.dart'; // after import 'package:internet_status/internet_status.dart';
The API is unchanged, so no other code needs to change. Keep the
extensionresoft import in files that also use its other features. If you stay
on extensionresoft ^1.5.1 you do not need this package.
Platform notes #
- The checks use
dart:io, so the package is for Android, iOS, macOS, Windows and Linux, not the web. - On Android,
connectivity_plusneeds theACCESS_NETWORK_STATEpermission (the plugin adds it) and the HTTP check needsINTERNET. - On iOS and macOS a VPN connection may be reported as
ConnectionType.other.
License #
BSD-3-Clause, see LICENSE.