How to Fix SocketException: Failed host lookup

Nur Sabilly
4 min readJul 4, 2023

--

Are you facing this issue when you’re developing your Flutter or Android apps? If so, then this article is for you. I decided to write this ‘how to solve’ article because this problem took me a lot of time to solve. I want you as Flutter and Android developer, don’t have to stick with this problem over time. I hope with this article can help you understand what it is and how to solve it, completely.

Before I start, I will explain in simple terms what does it mean. It is important to know the problem we’re facing before we finally decide to solve it. Let’s go!

What is SocketException actually?

Based on Flutter documentation in this page, SocketException is the type of exception thrown when a socket operation fails. A socket is a communication endpoint used for network communication. So, there was a problem in our network.

Typically, there are various reasons for SocketException to occur.

  1. Network Unavailability
    If there is no network connectivity.
  2. Connection Refused
    It occurs when trying to establish a connection with a server that is not listening to the request.
  3. Connection Timeout
    Client unable to connect to server within a specified time limit.
  4. DNS Resolution Failure
    When the domain name cannot be resolved to an IP address.

At least there are 4 common things that can be a reason for SocketException to occur. The next question is …

What is the real cause of my problem? Which one?

When I was developing my Flutter app on my machine, I was always using an Android emulator to simulated how it worked, was it really working? I realized there was a problem on my machine is when the app is trying to fetch images from the network using Image.network.

SocketException in Android Emulator.

It failed to look up the host. I was searching it through Google and then in a StackOverflow thread. I got the following answer.

1. Make sure you are online whether it is mobile or emulator

2. Make sure you have given internet permission in your app’s android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

I had checked on my project and it existed there. I conclude that the problem was not related to the project. It was not project-specific problem.

Next, I checked the image I was trying to fetch in Flutter and opened it up in my browser. The image was displayed correctly. Hmm. OK. The image was not corrupted.

After that, I checked the network connectivity on my Android Emulator. The Android Emulator I was using is Pixel 3A API 33 x86 64. How did I check it? First, I got my eyes on Android’s Quick Setting Dropdown. It connected to the internet! Just to increase my confidence I did really second check to opened up the browser, typed up something, and boom! Google search result was showing up properly. I think it was not coming up from the Emulator.

Here I made a mistake. I did not check to open the image in the emulator’s browser. I ended up completely numb because of this.

When I opened the image in emulator’s browser, it returns error.

dns_probe_finished_nxdomain

Why did it work for some url like https://google.com and did not work for the image? I realized that the domain cannot be resolved by emulator’s network. The error indicates that the DNS probing to the DNS server for a particular domain name’s corresponding IP was completed. This probe could not find the matching IP for the domain, and it determined that the domain is Non-Existent (NXDOMAIN).

It was DNS Resolution Failure!

How to solve it then?

It’s pretty straightforward.

  1. Use Public DNS. I used Google Public DNS

Configure your network settings to use the IP addresses 8.8.8.8 and 8.8.4.4 as your DNS servers.

2. If it’s possible, apply the number one on your emulator.

If those two doesn’t fix your problem. Here’s the list of possible solutions.

  • Use Google Public DNS or any Public DNS Provider on your machine and emulator.
  • Add internet permission in your AndroidManifest.xml.
    <uses-permission android:name="android.permission.INTERNET"/>
  • Turn vulkan off on emulator or chrome.
    1. Edit a file called advancedFeatures.ini in your android path in your machine. If it doesn’t exist, make a new one.
    Windows:
    C:\Users\<user>\.android
    Linux:
    ~/.android
    2. Add this code.
    Vulkan = off
    GLDirectMem = on
  • Restart your emulator. Cold Boot it.
  • Make sure your emulator can connect to internet.

In conclusion, solving errors in programming is not just about fixing the immediate issue, but also about building a deeper understanding of the problem and the code. By following a systematic approach, we can effectively troubleshoot errors and enhance our problem-solving skills.

If you find it useful please love and share it. I appreciate it a lot. Thank you.

--

--

Nur Sabilly

Full Stack Web Developer. Arch Linux User. Learn, Share, Socialize.