I'm getting internet status in my flutter web app, by implementing the following code in my service:
```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();
}
}
but I get the following error:
'''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)'''```
I already tried changing dart:io to universal:io but I get the following error:
'''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)'''
could you help me with some options to be able to solve it in flutter web, since in mobile I have no problem. Thank you.
I was also facing this issue because I was using following method to check internet connection:
Future<bool> hasNetwork() async {
try {
final result = await InternetAddress.lookup('example.com');
return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
} on SocketException catch (_) {
return false;
}}
Then I changed this method to:
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;
}
}
and it solved my issue.