In this example, we are going to show you how to check the internet connection in Flutter. You will learn to check if the device's internet connection is online or offline, if the device is online, then check if its connection with mobile data, wifi, wired ethernet, or Bluetooth is threatening. See the example below:
First, add connectivity_plus package in your project by adding the following lines in pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
connectivity_plus: ^2.2.1
Now, import the package in your script:
import 'package:connectivity_plus/connectivity_plus.dart';
var result = await Connectivity().checkConnectivity();
if(result == ConnectivityResult.mobile) {
print("Internet connection is from Mobile data");
}else if(result == ConnectivityResult.wifi) {
print("internet connection is from wifi");
}else if(result == ConnectivityResult.ethernet){
print("internet connection is from wired cable");
}else if(result == ConnectivityResult.bluetooth){
print("internet connection is from bluethooth threatening");
}else if(result == ConnectivityResult.none){
print("No internet connection");
}
Here, you can check if the connection is online or offline, if the internet connection is online then check if it is connected via mobile data, wifi, wired ethernet, or Bluetooth threatening.
You subscribe to the internet connection status update stream so that you can show the automatic offline or online messages on your app. For example:
import 'dart:async';
StreamSubscription? connection;
bool isoffline = false;
connection = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
// whenevery connection status is changed.
if(result == ConnectivityResult.none){
//there is no any connection
setState(() {
isoffline = true;
});
}else if(result == ConnectivityResult.mobile){
//connection is mobile data network
setState(() {
isoffline = false;
});
}else if(result == ConnectivityResult.wifi){
//connection is from wifi
setState(() {
isoffline = false;
});
}else if(result == ConnectivityResult.ethernet){
//connection is from wired connection
setState(() {
isoffline = false;
});
}else if(result == ConnectivityResult.bluetooth){
//connection is from bluetooth threatening
setState(() {
isoffline = false;
});
}
});
You can show messages on your widget tree like below:
Container(
color: isoffline?Colors.red:Colors.lightGreen,
//red color on offline, green on online
padding:EdgeInsets.all(10),
child: Text(isoffline?"Device is Offline":"Device is Online",
style: TextStyle(
fontSize: 20, color: Colors.white
),
),
)
For More Details: How to Show Automatic Internet Connection Offline Message in Flutter
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
runApp(MyApp());
}
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget{
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
StreamSubscription? connection;
bool isoffline = false;
@override
void initState() {
connection = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
// whenevery connection status is changed.
if(result == ConnectivityResult.none){
//there is no any connection
setState(() {
isoffline = true;
});
}else if(result == ConnectivityResult.mobile){
//connection is mobile data network
setState(() {
isoffline = false;
});
}else if(result == ConnectivityResult.wifi){
//connection is from wifi
setState(() {
isoffline = false;
});
}else if(result == ConnectivityResult.ethernet){
//connection is from wired connection
setState(() {
isoffline = false;
});
}else if(result == ConnectivityResult.bluetooth){
//connection is from bluetooth threatening
setState(() {
isoffline = false;
});
}
});
super.initState();
}
@override
void dispose() {
connection!.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Check Internet Connection"),
backgroundColor: Colors.redAccent,
),
body: Container(
alignment: Alignment.center,
child: Column(
children:[
Container(
width: double.infinity,
alignment: Alignment.center,
margin: EdgeInsets.only(bottom:30),
color: isoffline?Colors.red:Colors.lightGreen,
//red color on offline, green on online
padding:EdgeInsets.all(10),
child: Text(isoffline?"Device is Offline":"Device is Online",
style: TextStyle(
fontSize: 20, color: Colors.white
),),
),
ElevatedButton(onPressed: () async {
var result = await Connectivity().checkConnectivity();
if(result == ConnectivityResult.mobile) {
print("Internet connection is from Mobile data");
}else if(result == ConnectivityResult.wifi) {
print("internet connection is from wifi");
}else if(result == ConnectivityResult.ethernet){
print("internet connection is from wired cable");
}else if(result == ConnectivityResult.bluetooth){
print("internet connection is from bluethooth threatening");
}else if(result == ConnectivityResult.none){
print("No internet connection");
}
},
child: Text("Check Internet Connection")
)
]
)
),
);
}
}
When Internet Connection is Online
When Internet Connection is Offline:
In this way, you can check if the internet connection on device is online or offline in Flutter app.
Please Wait...
No any Comments on this Article