我在我的flutter web应用程序中获得互联网状态,通过在我的服务中实现以下代码:
```import 'dart:async';
import 'dart:io';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:rxdart/rxdart.dart';
enum ConnectionStatus {
online,
offline,
}
class CheckInternetConnection {
final Connectivity _connectivity = Connectivity();
/// We assume the initial status is Online
final _controller = BehaviorSubject.seeded(ConnectionStatus.online);
StreamSubscription? _connectionSubscription;
CheckInternetConnection() {
_checkInternetConnection();
}
Stream<ConnectionStatus> internetStatus() {
_connectionSubscription ??= _connectivity.onConnectivityChanged
.listen((_) => _checkInternetConnection());
return _controller.stream;
}
Future<void> _checkInternetConnection() async {
try {
// Sometimes the callback is called when we reconnect to wifi,
// but the internet is not really functional
// This delay try to wait until we are really connected to internet
final result = await InternetAddress.lookup('www.google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
_controller.sink.add(ConnectionStatus.online);
} else {
_controller.sink.add(ConnectionStatus.offline);
}
} on SocketException catch (_) {
_controller.sink.add(ConnectionStatus.offline);
}
}
Future<void> close() async {
await _connectionSubscription?.cancel();
await _controller.close();
}
}
但我得到以下错误:
'''Error: Unsupported operation: InternetAddress.lookup
at Object.throw_ [as throw] (http://localhost:50773/dart_sdk.js:5080:11)
at InternetAddress.lookup (http://localhost:50773/dart_sdk.js:59860:17)
at internet_conection_service.CheckInternetConnection.new._checkInternetConnection (http://localhost:50773/packages/flutter_web/src/core/services/internet_conection_service.dart.lib.js:86:50)
at _checkInternetConnection.next (<anonymous>)
at runBody (http://localhost:50773/dart_sdk.js:40660:34)
at Object._async [as async] (http://localhost:50773/dart_sdk.js:40691:7)
at [_checkInternetConnection] (http://localhost:50773/packages/flutter_web/src/core/services/internet_conection_service.dart.lib.js:84:20)'''```
我已经尝试将dart:io更改为universal:io,但我得到了以下错误:
'''Error: UnimplementedError
at Object.throw_ [as throw] (http://localhost:50773/dart_sdk.js:5080:11)
at InternetAddress.lookup (http://localhost:50773/packages/universal_io/src/io/sync_socket.dart.lib.js:3038:24)
at internet_conection_service.CheckInternetConnection.new._checkInternetConnection (http://localhost:50773/packages/flutter_web/src/core/services/internet_conection_service.dart.lib.js:87:64)
at _checkInternetConnection.next (<anonymous>)
at runBody (http://localhost:50773/dart_sdk.js:40660:34)'''
你能帮我一些选项,以便能够解决它在扑动网页,因为在移动我没有问题。谢谢你。
我也面临这个问题,因为我使用以下方法来检查互联网连接:
Future<bool> hasNetwork() async {
try {
final result = await InternetAddress.lookup('example.com');
return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
} on SocketException catch (_) {
return false;
}}
然后我把这个方法改为:
Future<bool> hasNetwork() async {
try {
final result = await http.get(Uri.parse('www.google.com'));
if(result.statusCode==200){
return true;
}
else{
return false;
}
}
on SocketException catch (_) {
return false;
}
}
,它解决了我的问题。
虽然接受的答案是有效的,而且很好,但我认为这是一个更好的方法:只在应用程序在浏览器上运行时使用接受的答案。默认情况下,使用更快的常规查找。
顺便说一下,knownUrl
可以是你的项目网站,如果你有一个。您必须确保CORS为您启用。如果您不确定,请尝试google.com
,example.com
和url。
Future<bool> hasNetwork(String knownUrl) async {
if (kIsWeb) {
return _hasNetworkWeb(knownUrl);
} else {
return _hasNetworkMobile(knownUrl);
}
}
Future<bool> _hasNetworkWeb(String knownUrl) async {
try {
final result = await http.get(Uri.parse(knownUrl));
if (result.statusCode==200) return true;
} on SocketException catch (_) {}
return false;
}
Future<bool> _hasNetworkMobile(String knownUrl) async {
try {
final result = await InternetAddress.lookup(knownUrl);
return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
} on SocketException catch (_) {}
return false;
}