加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 14:57:59 所属栏目:百科 来源:网络整理
导读:我有一个网络电话要执行.但在此之前,我需要检查设备是否具有互联网连接. 这是我到目前为止所做的: var connectivityResult = new Connectivity().checkConnectivity();// User defined class if (connectivityResult == ConnectivityResult.mobile || conne
我有一个网络电话要执行.但在此之前,我需要检查设备是否具有互联网连接.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读