flutterでモバイル/Wifiのようなネットワーク接続を確認する方法

今回は、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



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム