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

前端之家收集整理的这篇文章主要介绍了dart – 检查Flutter应用程序上是否有可用的Internet连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个网络电话要执行.但在此之前,我需要检查设备是否具有互联网连接.

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

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

以上方法不起作用.

解决方法

连接插件在其文档中声明,它仅在有网络连接时提供信息,但如果网络连接到Internet则不提供

Note that on Android,this does not guarantee connection to Internet. For instance,the app might have wifi access but it might be a VPN or a hotel WiFi with no access.

您可以使用

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');
}

猜你在找的Flutter相关文章