我有一个要执行的网络调用。但在此之前,我需要检查设备是否有互联网连接。
这是我到目前为止所做的:
var connectivityResult = new Connectivity().checkConnectivity();// User defined class
if (connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi) {*/
this.getData();
} else {
neverSatisfied();
}
上述方法无效。
最佳答案
connectivity插件在其文档中声明它仅在有网络连接时提供信息,但在网络连接到 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('example.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
}
} on SocketException catch (_) {
print('not connected');
}
connectivity包已弃用。使用官方 Flutter 社区 connectivity_plus而是打包。
https://stackoverflow.com/questions/49648022/