In this example, we are going to show you how to check the internet network connection type whether it is from mobile date, wifi connection, Bluetooth connection, or ethernet connection in Flutter. This example can be used to check the internet connection too.
First, you need to add connectivity_plush package to your project by adding the following lines in pubspec.yaml
dependencies:
flutter:
sdk: flutter
connectivity_plus: ^2.3.6
Import package to your script:
import 'package:connectivity_plus/connectivity_plus.dart';
String cType = "none";
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
cType = "Mobile Data";
} else if (connectivityResult == ConnectivityResult.wifi) {
cType = "Wifi Network";
}else if(connectivityResult == ConnectivityResult.ethernet){
cType = "Ethernet Network";
}else if(connectivityResult == ConnectivityResult.bluetooth){
cType = "Blutooth Data connection";
}else{
cType = "none";
}
print(cType); //Output: Wifi Network
See this also: How to Check Internet Connection in Flutter
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
void main(){
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> {
String cType = "none";
@override
void initState() {
checkConnection();
//listen to the network connection type change
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
if (result == ConnectivityResult.mobile) {
cType = "Mobile Data";
} else if (result == ConnectivityResult.wifi) {
cType = "Wifi Network";
}else if(result == ConnectivityResult.ethernet){
cType = "Ethernet Network";
}else if(result == ConnectivityResult.bluetooth){
cType = "Blutooth Data connection";
}else{
cType = "none";
}
setState(() {
//refresh UI
});
});
super.initState();
}
checkConnection() async{
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
cType = "Mobile Data";
} else if (connectivityResult == ConnectivityResult.wifi) {
cType = "Wifi Network";
}else if(connectivityResult == ConnectivityResult.ethernet){
cType = "Ethernet Network";
}else if(connectivityResult == ConnectivityResult.bluetooth){
cType = "Blutooth Data connection";
}else{
cType = "none";
}
setState(() {
//refresh UI
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Check Connection Type"),
backgroundColor: Colors.redAccent
),
body: Container(
padding: EdgeInsets.only(top:20, left:20, right:20),
alignment: Alignment.topCenter,
child: Column(
children: [
Text("Connection Type: $cType", style: TextStyle(fontSize: 20),),
],)
)
);
}
}
In this way, you can check the network connection type either it is connected with mobile data or wifi connection in Flutter.
Please Wait...
No any Comments on this Article