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.
- Network Unavailability
If there is no network connectivity.
- Connection Refused
It occurs when trying to establish a connection with a server that is not listening to the request.
- Connection Timeout
Client unable to connect to server within a specified time limit.
- 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.
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
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.
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.
- Use Public DNS. I used Google Public DNS
Configure your network settings to use the IP addresses 22.214.171.124 and 126.96.36.199 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.
- 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.
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.