SocketException: No address associated with hostname

SocketException: Failed host lookup: 'api.example.com' (OS Error: No address associated with hostname, errno = 7)

Quick Answer

DNS resolution failed, usually because there is no internet connection.

Why This Happens

In Flutter, this SocketException occurs when DNS cannot resolve the hostname. This typically means the device has no internet connection, the hostname is wrong, or DNS servers are unreachable. Always handle network errors gracefully in your app.

The Problem

Future<String> fetchData() async {
  final response = await http.get(Uri.parse('https://api.example.com/data'));
  return response.body;
}

The Fix

Future<String> fetchData() async {
  try {
    final response = await http.get(Uri.parse('https://api.example.com/data'));
    return response.body;
  } on SocketException {
    throw Exception('No internet connection. Please check your network.');
  }
}

Step-by-Step Fix

  1. 1

    Identify the error

    Look at the SocketException about failed host lookup. This indicates a DNS resolution failure.

  2. 2

    Find the cause

    Check internet connectivity, verify the hostname is correct, and ensure the device can reach DNS servers.

  3. 3

    Apply the fix

    Add SocketException handling around HTTP calls and show a user-friendly offline message.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free