flutter network_error ai_generated partial

NetworkError: DNS lookup failed for host 'example.com'

ID: flutter/networkerror-dns-lookup-failed

Also available as: JSON · Markdown · 中文
80%Fix Rate
81%Confidence
1Evidence
2023-03-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Flutter 2.10 active
Flutter 3.0 active
Flutter 3.7 active

Root Cause

The device could not resolve the hostname to an IP address due to network connectivity issues, incorrect DNS configuration, or the host being unreachable.

generic

中文

设备无法将主机名解析为 IP 地址,原因是网络连接问题、DNS 配置错误或主机不可达。

Official Documentation

https://api.flutter.dev/flutter/dart-io/HttpClient-class.html

Workarounds

  1. 85% success Check the device's network connectivity and retry with exponential backoff: import 'dart:io'; Future<bool> checkConnectivity() async { try { final result = await InternetAddress.lookup('example.com'); return result.isNotEmpty && result[0].rawAddress.isNotEmpty; } on SocketException catch (_) { return false; } }
    Check the device's network connectivity and retry with exponential backoff:
    import 'dart:io';
    
    Future<bool> checkConnectivity() async {
      try {
        final result = await InternetAddress.lookup('example.com');
        return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
      } on SocketException catch (_) {
        return false;
      }
    }
  2. 75% success Use a custom DNS resolver or fallback to a different DNS server, e.g., Google's 8.8.8.8, by configuring the HttpClient: HttpClient client = HttpClient(); client.findProxy = (uri) { return 'PROXY 8.8.8.8:53; DIRECT'; };
    Use a custom DNS resolver or fallback to a different DNS server, e.g., Google's 8.8.8.8, by configuring the HttpClient:
    HttpClient client = HttpClient();
    client.findProxy = (uri) {
      return 'PROXY 8.8.8.8:53; DIRECT';
    };
  3. 80% success Implement a retry mechanism with a timeout and user-friendly error message: try { await http.get(Uri.parse('https://example.com')).timeout(Duration(seconds: 10)); } on SocketException { // show error to user }
    Implement a retry mechanism with a timeout and user-friendly error message:
    try {
      await http.get(Uri.parse('https://example.com')).timeout(Duration(seconds: 10));
    } on SocketException {
      // show error to user
    }

中文步骤

  1. Check the device's network connectivity and retry with exponential backoff:
    import 'dart:io';
    
    Future<bool> checkConnectivity() async {
      try {
        final result = await InternetAddress.lookup('example.com');
        return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
      } on SocketException catch (_) {
        return false;
      }
    }
  2. Use a custom DNS resolver or fallback to a different DNS server, e.g., Google's 8.8.8.8, by configuring the HttpClient:
    HttpClient client = HttpClient();
    client.findProxy = (uri) {
      return 'PROXY 8.8.8.8:53; DIRECT';
    };
  3. Implement a retry mechanism with a timeout and user-friendly error message:
    try {
      await http.get(Uri.parse('https://example.com')).timeout(Duration(seconds: 10));
    } on SocketException {
      // show error to user
    }

Dead Ends

Common approaches that don't work:

  1. Hardcoding IP addresses instead of hostnames 50% fail

    IP addresses can change; not a scalable solution and may break on different networks.

  2. Disabling DNS resolution and using raw IP in the app code 60% fail

    Same as above; also violates best practices and may cause security warnings.

  3. Assuming the error is always on the server side 40% fail

    Often the client's DNS configuration or network is the issue; server may be fine.