The problem with the connectivy plugins it doesn't check for internetconnection means,you may be connected to a wifi but there will no internet,same goes with mobile data,it just check if wifi or mobile data is on or off,so little bit searching i have found multiple codes and implemented in my app,it will listen for the internet connection
Install these packages and import the packages
connectivity
fluttertoast:
data_connection_checker:
Initialize variables
var subscription;
var connectionStatus;
String _connectionStatus = 'Unknown';
final Connectivity _connectivity = Connectivity();
StreamSubscription<ConnectivityResult> _connectivitySubscription;
InitState and functions for checking connections
@override
void initState() {
initConnectivity();
_connectivitySubscription =
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
checkInternetConnectivity();
super.initState();
}
Future<bool> checkInternetConnectivity() async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
// I am connected to a mobile network, make sure there is actually a net connection.
if (await DataConnectionChecker().hasConnection) {
// Mobile data detected & internet connection confirmed.
return Fluttertoast.showToast(
msg: "Connected to Mobile Network",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 16.0
);
return true;
} else {
// Mobile data detected but no internet connection found.
return Fluttertoast.showToast(
msg: "Mobile data detected but no internet connection found",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
return false;
}
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a WIFI network, make sure there is actually a net connection.
if (await DataConnectionChecker().hasConnection) {
// Wifi detected & internet connection confirmed.
return Fluttertoast.showToast(
msg: "Connected to WIFI Network",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 16.0
);
return true;
} else {
// Wifi detected but no internet connection found.
return Fluttertoast.showToast(
msg: "Wifi detected but no internet connection found.",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
return false;
}
} else {
// Neither mobile data or WIFI detected, not internet connection found.
return Fluttertoast.showToast(
msg: "No Internet connection found",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
}
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initConnectivity() async {
ConnectivityResult result;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await _connectivity.checkConnectivity();
} on PlatformException catch (e) {
print(e.toString());
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) {
return Future.value(null);
}
return _updateConnectionStatus(result);
}
//Constantly check for the status of the connection
Future<void> _updateConnectionStatus(ConnectivityResult result) async {
if(result==ConnectivityResult.mobile)
{
if (await DataConnectionChecker().hasConnection) {
// Mobile data detected & internet connection confirmed.
return Fluttertoast.showToast(
msg: "Connected to Mobile Network",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 16.0
);
return true;
} else {
// Mobile data detected but no internet connection found.
return Fluttertoast.showToast(
msg: "Mobile data detected but no internet connection found",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
return false;
}
}
else if(result==ConnectivityResult.mobile){
if (await DataConnectionChecker().hasConnection) {
// Wifi detected & internet connection confirmed.
return Fluttertoast.showToast(
msg: "Connected to WIFI Network",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.green,
textColor: Colors.white,
fontSize: 16.0
);
return true;
} else {
// Wifi detected but no internet connection found.
return Fluttertoast.showToast(
msg: "Wifi detected but no internet connection found.",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
return false;
}
}
else if(result==ConnectivityResult.none){
if (await DataConnectionChecker().hasConnection) {
// Wifi detected & internet connection confirmed.
return Fluttertoast.showToast(
msg: "No Internet connection found",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
return true;
} else {
// Wifi detected but no internet connection found.
return Fluttertoast.showToast(
msg: "No Internet connection found",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
return false;
}
}
}
@override
void dispose() {
_connectivitySubscription.cancel();
super.dispose();
}