flutter_connectivity
Constantly monitors and reports changes in connection quality to a specific server or service, built in pure Dart and compatible with Dart and Flutter.
Unlike other packages that only check internet connectivity, this one ensures actual internet access by testing against your designated server.
Features
x
Select a server to test against (eg: example.com)x
Configure interval for testingx
Configure how many failed tests before reporting a connection lossx
Set latency thresholds for different connection qualitiesx
Get notified of connection quality changes in real-timex
Pause and resume monitoringx
Query connection historyx
Accommodates occasional network hiccups
Use this package when you need to:
- monitor connection quality
- maintain a stable connection
- detect network issues
- provide real-time feedback to users (eg: connection quality indicator)
Getting started
- Install the package
Dart
dart pub add flutter_connectivity
Flutter
flutter pub add flutter_connectivity
- Import the package
import 'package:flutter_connectivity/flutter_connectivity.dart';
- Create a new instance of
FlutterConnectivity
and start monitoring
// instantiate
FlutterConnectivity connectivity = FlutterConnectivity(endpoint: 'https://example.com');
// listen
connectivity.listenToLatencyChanges((ConnectivityStatus status, int latency) {
print('Connection status: $status, latency: $latency');
});
Screenshot
Compatibility
Works with all Dart and Flutter (web, mobile, desktop) projects.
Usage
Create a new instance of FlutterConnectivity
// Modify the endpoint to your server
FlutterConnectivity connectivity = FlutterConnectivity(endpoint: 'https://example.com');
Optional: Configure the connectivity instance
connectivity.configure(
// How many failed requests before reporting a connection loss
allowedFailedRequests: 2,
// How often to check the connection
checkInterval: const Duration(seconds: 3),
// What kind of logs appear on your console
logLevel: LogLevel.error,
);
Optional: Configure latency thresholds
// Set latency thresholds in milliseconds
connectivity.setLatencyThresholds(
disconnected: 10000,
slow: 5000,
moderate: 2000,
fast: 1000,
);
Start the connectivity monitor
connectivity.listenToLatencyChanges((ConnectivityStatus status, int latency) {
print('Connection status: $status, latency: $latency');
});
Pause and resume monitoring
// Pause monitoring
connectivity.pause();
// Resume monitoring
connectivity.resume();
Get history of latencies with timestamps
Map<int, int> latencies = connectivity.latencyHistory;
Stop monitoring
connectivity.dispose();
Check out the example for a complete implementation. Run the example using the following command:
dart example/example.dart
Additional information
Report issues and help improve the package on GitHub. Check API reference for more detailed information.