feat: Adds utility to check internet connection

Provides a helper function `checkInternetConnexion` using the `connectivity_plus` package. It returns true for network types typically used for internet access (mobile, wifi, ethernet, vpn) and false otherwise.
This commit is contained in:
mandreshope 2025-06-25 10:01:33 +03:00
parent 812ece1ba8
commit 0d3e3a033d

View File

@ -1,3 +1,4 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
extension StatefulWidgetExtensions on State<StatefulWidget> {
@ -9,3 +10,40 @@ extension StatefulWidgetExtensions on State<StatefulWidget> {
}
}
}
Future<bool> checkInternetConnexion() async {
final List<ConnectivityResult> connectivityResult = await (Connectivity()
.checkConnectivity());
// This condition is for demo purposes only to explain every connection type.
// Use conditions which work for your requirements.
if (connectivityResult.contains(ConnectivityResult.mobile)) {
// Mobile network available.
return true;
} else if (connectivityResult.contains(ConnectivityResult.wifi)) {
// Wi-fi is available.
// Note for Android:
// When both mobile and Wi-Fi are turned on system will return Wi-Fi only as active network type
return true;
} else if (connectivityResult.contains(ConnectivityResult.ethernet)) {
// Ethernet connection available.
return true;
} else if (connectivityResult.contains(ConnectivityResult.vpn)) {
// Vpn connection active.
// Note for iOS and macOS:
// There is no separate network interface type for [vpn].
// It returns [other] on any device (also simulator)
return true;
} else if (connectivityResult.contains(ConnectivityResult.bluetooth)) {
// Bluetooth connection available.
return false;
} else if (connectivityResult.contains(ConnectivityResult.other)) {
// Connected to a network which is not in the above mentioned networks.
return false;
} else if (connectivityResult.contains(ConnectivityResult.none)) {
// No available network types
return false;
} else {
return false;
}
}