# NetworkError：主机 'example.com' 的 DNS 查找失败

- **ID:** `flutter/networkerror-dns-lookup-failed`
- **领域:** flutter
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Flutter 2.10 | active | — | — |
| Flutter 3.0 | active | — | — |
| Flutter 3.7 | active | — | — |

## 解决方案

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
}
   ```

## 无效尝试

- **Hardcoding IP addresses instead of hostnames** — IP addresses can change; not a scalable solution and may break on different networks. (50% 失败率)
- **Disabling DNS resolution and using raw IP in the app code** — Same as above; also violates best practices and may cause security warnings. (60% 失败率)
- **Assuming the error is always on the server side** — Often the client's DNS configuration or network is the issue; server may be fine. (40% 失败率)
