Search code examples
flutterdartexceptiontry-catchsocketexception

Unhandled Exception: SocketException flutter


The goal is to get website from email. I want to catch Socket Exceptions in a try Catch statement, but the following error wasn't caught and no print statement.

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: SocketException: Failed host lookup: 'subdomain.domain.com' (OS Error: nodename nor servname provided, or not known, errno = 8).

Following the flutter documentation here and here

here is my code.

import 'package:http/http.dart' as http;

Future<void> getWebsite(email) async {
    try {
      // get domain from email
      String _dom = new RegExp(r"(?<=[@]).+").stringMatch(email);
      // Get response status code from url
      final response = await http.get("https://$_dom");
      // Set Domain
      response.statusCode == 200 ? setDomain("https://$_dom") : setDomain(null);
    } on SocketException catch (_) {
      print("socket error");
    } on HttpException catch (_) {
      print("http error");
    } on FormatException catch (_) {
      print("format exception");
    } catch (e) {
      print("any exception");
    }
}

Here is the result of flutter doctor -v

[✓] Flutter (Channel master, 1.22.0-10.0.pre.153, on Mac OS X 10.15.5 19F101, locale en-US) • Flutter version 1.22.0-10.0.pre.153 at /Users/sean/developer/flutter • Framework revision 2e643651a9 (4 months ago), 2020-09-11 23:07:03 -0400 • Engine revision 16b900b63e • Dart version 2.10.0 (build 2.10.0-117.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc1) • Android SDK at /Users/sean/Library/Android/sdk • Platform android-29, build-tools 30.0.0-rc1 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 11.3, Build version 11C29 • CocoaPods version 1.9.1

[✓] Android Studio (version 3.6) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin version 45.1.1 • Dart plugin version 192.7761 • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.52.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.18.0

[✓] Connected device (1 available)
• iPhone 11 Pro (mobile) • E280A6FB-4AF7-4409-B79E-FA4E652C4FE0 • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-3 (simulator)

• No issues found!

Here is what I see in the console

Launching lib/main.dart on iPhone 11 Pro in debug mode...
Running pod install...
Running Xcode build...
Xcode build done.                                           552.8s
Waiting for iPhone 11 Pro to report its views...
Debug service listening on ws://127.0.0.1:58043/ZgdnQH-v_BY=/ws
Syncing files to device iPhone 11 Pro...
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: SocketException: Failed host lookup: 'av.abbott.com' (OS Error: nodename nor servname provided, or not known, errno = 8)
#0      _NativeSocket.lookup.<anonymous closure> (dart:io-patch/socket_patch.dart:502:9)
#1      _rootRunUnary (dart:async/zone.dart:1198:47)
#2      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
#3      _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
#4      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
#5      Future._propagateToListeners (dart:async/future_impl.dart:725:32)
#6      Future._completeWithValue (dart:async/future_impl.dart:529:5)
#7      Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:567:7)
#8      _rootRun (dart:async/zone.dart:1190:13)
#9      _CustomZone.run (dart:async/zone.dart:1093:19)
#10     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
#11     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:as<…>

Solution

  • I changed to stable channel which fixed the issue.

    https://flutter.dev/docs/development/tools/sdk/upgrading#switching-flutter-channels

    flutter channel stable

    flutter upgrade