Error: Unsupported operation: InternetAddress.lookup Flutter Web
Asked Answered
H

3

9

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.

Heyward answered 28/6, 2022 at 19:6 Comment(1)
yep, it's not implemented at all in universal_io: github.com/dint-dev/universal_io/blob/master/lib/src/io/… you can create an issue in their bug tracker and ask if they know why this is not implemented, it's probably because it's hard to do on the web... perhaps try to use Dart's JS support to do this using JS when running on the web.Promisee
B
9

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.

Bonnette answered 18/8, 2022 at 7:29 Comment(4)
I do face CORS issues with this solution (using example.com instead of google since google.com is blocked in some countries). Any idea/recommendation?Scab
use any website URL other than google.Bonnette
Yey, take into account that those methods don't really do the same. The lookup can be fast (because it only uses a reverse DNS lookup) while the actual GET request can be slow (because it depends on the address actually answering the request) I'll make a Frankenstein of this answer to suggest a better one. Of course, thanks for the answer @MuhammadUmairSaqib!!!Brote
we can use lookup for mobile but for web it doesn't work. Then need to use GET request.Bonnette
B
1

Although the accepted answer works and is good, here's what I think it's a better approach: only use the accepted answer when the app runs on the browser. By default, use the regular lookup, which is faster.

By the way, knownUrl could be your project website if you have one. You have to be sure is CORS enabled for you. If you are not sure about it, just try google.com, example.com and urls like that.

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;
}
Brote answered 26/4, 2023 at 13:4 Comment(0)
H
0

this error show of you because there some problems in your package (you used to connect with internet ) so use this package internet_connection_checker_plus instead of internet_connection_checker

Halve answered 4/5, 2024 at 23:0 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Jemma

© 2022 - 2025 — McMap. All rights reserved.