flutterでモバイル/Wifiのようなネットワーク接続を確認する方法
- 2022年10月14日
- 技術情報

今回は、Flutterのインターネット接続の種類が、モバイルデート、wifi接続、Bluetooth接続、Ethernet接続のいずれであるかを確認する方法をご紹介します。
まずはpubspec.yaml ファイルにパッケージを追加する。
connectivity_plus: ^2.3.6
インターネット接続の種類を確認する方法
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(() {
});
}
上記の機能をinitState()で呼び出します。
void initState() {
checkConnection();
super.initState();
}
表示ようコード
Scaffold(
appBar: AppBar(
title: const Text("Check Network Connection Type"),
backgroundColor: Colors.blueAccent
),
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),),
],)
)
);
金曜担当 – Ami
asahi at 2022年10月14日 10:00:00