小编典典

检查Flutter应用程序上是否有Internet连接可用

flutter

我有一个网络电话要执行。但是在此之前,我需要检查设备是否具有互联网连接。

这是我到目前为止所做的:

  var connectivityResult = new Connectivity().checkConnectivity();// User defined class
    if (connectivityResult == ConnectivityResult.mobile ||
        connectivityResult == ConnectivityResult.wifi) {*/
    this.getData();
    } else {
      neverSatisfied();
    }

上面的方法不起作用。


阅读 387

收藏
2020-08-13

共1个答案

小编典典

连接插件的状态在其文档是否有网络连接,但不是如果网络连接到互联网,它仅提供信息

请注意,在Android上,这不保证可以连接到Internet。例如,该应用程序可能具有wifi访问权限,但可能是VPN或无法访问的酒店WiFi。

您可以使用

import 'dart:io';
...
try {
  final result = await InternetAddress.lookup('google.com');
  if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
    print('connected');
  }
} on SocketException catch (_) {
  print('not connected');
}
2020-08-13