Skip to content

Latest commit

 

History

History
125 lines (99 loc) · 3.3 KB

README.md

File metadata and controls

125 lines (99 loc) · 3.3 KB

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

  • Select a server to test against (eg: example.com)
  • Configure interval for testing
  • Configure how many failed tests before reporting a connection loss
  • Set latency thresholds for different connection qualities
  • Get notified of connection quality changes in real-time
  • Pause and resume monitoring
  • Query connection history
  • 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

  1. Install the package

Dart

dart pub add flutter_connectivity

Flutter

flutter pub add flutter_connectivity
  1. Import the package
import 'package:flutter_connectivity/flutter_connectivity.dart';
  1. 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

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.