我有一个网络电话要执行.但在此之前,我需要检查设备是否具有互联网连接.
这是我到目前为止所做的:
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'); }