Flutter Network Connectivity
A flutter plugin to check for Internet Availability as a stream based on network connectivity status, periodic interval or on call.
Usage
First, add flutter_network_connectivity
as a dependency in your pubspec.yaml file.
dependencies:
flutter_network_connectivity: ^0.0.6
Don't forget to flutter pub get
.
Then import:
import 'package:flutter_network_connectivity/flutter_network_connectivity.dart';
Now you can create FlutterNetworkConnectivity object and use its methods
FlutterNetworkConnectivity flutterNetworkConnectivity =
FlutterNetworkConnectivity(
isContinousLookUp: true, // optional, false if you cont want continous lookup
lookUpDuration: const Duration(seconds: 5), // optional, to override default lookup duration
lookUpUrl: 'example.com', // optional, to override default lookup url
);
To Check for Stream of Network Connectivity Status
_flutterNetworkConnectivity.getInternetAvailabilityStream().listen((isInternetAvailable) {
// do something
});
Then Register Availability Listener after setting up Stream Listerer
await _flutterNetworkConnectivity.registerAvailabilityListener();
Unregister on dispose
await _flutterNetworkConnectivity.unregisterAvailabilityListener();
To Check for Internet Availability on Call
bool _isNetworkConnectedOnCall = await
_flutterNetworkConnectivity.isInternetConnectionAvailable();
Examples
Simple usage example can be found in the example folder.
Android
Uses permission
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Minimum SDK Version 16
iOS
Uses NetworkMonitor, minimum required version 12.0
Demo
About
This plugin uses NetworkCapabilities for Android and NetworkMonitor for iOS to check for network connectivity status.
Contributing?
You're always welcome. See Contributing Guidelines. You can also take a look at Status Tracker to know more information about current or pending features/issues.