Nea*_*sva 7 dart flutter flutter-web
我通过在我的服务中实现以下代码,在我的 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();
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
'''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)'''```
Run Code Online (Sandbox Code Playgroud)
我已经尝试将 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)'''
Run Code Online (Sandbox Code Playgroud)
你能帮我提供一些选项,以便能够在 flutter web 中解决它,因为在移动设备中我没有问题。谢谢。
我也遇到了这个问题,因为我使用以下方法来检查互联网连接:
Future<bool> hasNetwork() async {
try {
final result = await InternetAddress.lookup('example.com');
return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
} on SocketException catch (_) {
return false;
}}
Run Code Online (Sandbox Code Playgroud)
然后我把这个方法改成了:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
它解决了我的问题。
| 归档时间: |
|
| 查看次数: |
3556 次 |
| 最近记录: |