146

Whenever I try to do an http call after about 20 seconds I get in the console the following error:

E/flutter ( 8274): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter ( 8274): SocketException: Failed host lookup: 'flutter-project-xxxxx.firebaseio.com' (OS Error: No address associated with hostname, errno = 7)

This error happens for every method and every route I call through the app http package.

I'm developing a flutter app on Windows, using an AVD virtual device from Android Studio.

Versions:

  http: ^0.12.0+1
  flutter: 1.0

Cases:

  1. From home or though tethering from my phone: connectivity works fine on every part of the virtual device

  2. From my work network (behind the firewall):

Only with Android Web View, I can browse the internet without any problem (i can even call the same url, I use in the code and it works). I get a connection error when using any other application (Chrome, Google Play, ecc...) in the virtual device, and specifically the "SocketException", when testing my app.

What is the difference between the calls coming from that app? Is there a way to route my app calls the same way as the ones in the webview?

Thanks!

FGH
  • 2,900
  • 6
  • 26
  • 59
David
  • 1,503
  • 2
  • 6
  • 7
  • 8
    Just wanted to add that this error can occur if you phone's connected to a WiFi with a firewall... this can block traffic (in my case it did for a corporate setting). Not always the first thought, but worth mentioning – Petro Dec 02 '20 at 21:06
  • 1
    I am using the real device, in my case, my mobile data is not on, so I just on my mobile data then the problem will be solved, so pleae check you'r connection with real device – Tushar Nikam Dec 05 '20 at 16:01
  • I wouldn't call this a surefire answer, but for me, disconnecting from the 5G connection I was on and connecting to a 2.4G connection fixed the problem. Not sure why. – Pyzard Jun 04 '22 at 08:51

38 Answers38

260

Adding internet permission is not only a solution.
You also have to make sure that you are online whether it is mobile or emulator

  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"/>

Ishwar Chandra Tiwari
  • 5,099
  • 6
  • 22
  • 41
  • 53
    Curiously, this works without the internet permission in debug mode – Sisir Mar 05 '20 at 08:37
  • 8
    If app needs access to network state i.e. allow app to access info about networks then following permission also needed. Refer for more info: https://developer.android.com/training/basics/network-ops/connecting – varad_s Apr 14 '20 at 09:15
  • @Choco like you do on your phone – Ishwar Chandra Tiwari Jul 27 '20 at 14:26
  • I found out that there exist multiple AndroidManifest.xml. At least one in /main and another in /debug. – Asad S Nov 05 '20 at 10:24
  • In my case, "Airplane mode" was on. Switched it off, and everything worked again (I can't explain what switched it on) – Mengrolek Feb 10 '21 at 14:43
  • 10
    There are multiple multiple AndroidManifest.xml, you need to make the difference between them. /debug/AndroidManifest.xml is for the debug app (in developement) and the /main/AndroidManifest.xml is for the app in production (APK) . You need to grant the access in both files. – tom hikari Apr 13 '21 at 09:20
  • 1
    I just lost one morning out of this... why the hell would work in the emulator, and even in debug mode on my personal phone, whereas it wouldn't in a staging build?? I'm so confused. – venir Oct 01 '21 at 10:09
  • 1
    @Sisir, because it has to be 'another' AndroidManifest.xml in your app (in my case i have this file in profile folder and it manages the permision) – fsalazar_sch Jan 22 '23 at 20:33
  • 2
    @Sisir When the app is compiled it merges all Android Manifests (From all your plugins and your main manifest). So even if you don't have certain permissions added, if the plugins Manifests have it, it will end up in the compiled manifest. – Pierre Feb 10 '23 at 14:37
  • For me, nothing works, i checked the wifi, 3G, everything... only works on my cellphone.. i checket the airplane mode to, and nothing. idk what else i can do – E. Greeff Jun 22 '23 at 20:03
41

Add <uses-permission android:name="android.permission.INTERNET" /> to your AndroidManifest.xml file.

Francis C
  • 511
  • 4
  • 7
27

Make sure you have added the in android/app/src/main/AndroidManifest.xml

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

enter image description here

22

I added <uses-permission android:name="android.permission.INTERNET" /> to my manifest. I then had to restart the emulator for internet to work.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Liping Xiong
  • 316
  • 2
  • 8
  • Same thing here, couldn't connect to the outside world via http and I also had that entry in the AndroidManifest.xml file. So, I restared the emulator and voila, worked great. – larryq Aug 05 '19 at 19:40
  • remember to restart the emulator for this to work. – FlyingSnowGhost May 26 '23 at 13:06
20

If you are using an emulator make sure that the mobile data is active

Sameh Khemira
  • 1,043
  • 1
  • 12
  • 20
14

You should try following:

Try run application in debug mode if its working fine and not working in release mode then it might be related to permission issue so add permissions(INTERNET, ACCESS_NETWORK_STATE) in manifest file.

  • your_project_root_directory/android/app/src/debug/AndroidManifest.xml
  • your_project_root_directory/android/app/src/main/AndroidManifest.xml
  • your_project_root_directory/android/app/src/profile/AndroidManifest.xml

If not included then add permissions:

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

If its not working in debug mode, then check either your url is correct by hitting in postman. If its working in postman then you should execute your webservice in this way:

Add dependency in pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter

  http: 0.13.3

After adding http:0.13.3 then click on Pub get button If its executed with code 0 then it means its successfully added to your project.

in your dart file import http.dart:

import 'package:http/http.dart';

Now write this code to execute webservice:

var url = Uri.parse('some-url-login?username=abc&&password=xyz');
var response = await get(url, headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': 'true',
          'Access-Control-Allow-Headers': 'Content-Type',
          'Access-Control-Allow-Methods':
          'GET,PUT,POST,DELETE'
        });
if (response.statusCode == 200) {
 // success
} else if(response.statusCode == 404){
 // not found
} else if(response.statusCode == 500){
 // server not responding.
} else {
 // some other error or might be CORS policy error. you can add your url in CORS policy. 
}

I hope it will help you. Chill!

Naimatullah
  • 3,749
  • 2
  • 13
  • 12
12

Go for AVD manager, then choose your emulator, on Actions menu click down option sign, then choose wipe data, then restart your emulator. It works for me.

Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
Fitsum Firew
  • 121
  • 1
  • 2
7

Even I had the same issue. I restarted my emulator and it worked!!

Joel Dsouza
  • 91
  • 1
  • 5
  • 2
    Please double check your phrasing "Even I ...". To readers with different language background that reads weirdly and could be misread (with some effort admittedly) as something disrepectful. – Yunnosch Sep 30 '20 at 14:43
  • thank you very much , your answer helped me more than one time – Mosa Mar 06 '23 at 08:30
6

In my case it was because my phone was not connected to internet

Just enable internet connexion

M E S A B O
  • 783
  • 1
  • 11
  • 15
6

In My case I opened the emulator before I connected to the WIFI to system.When network(WIFI) has connected to my system but unable to connect to emulator . Due to no internet access to the Emulator,this issues occurs.I resolved this issues by this way.

Solutions:

Connected network access to the system then I Closed the emulator and re-open it. Install applications ,run application and open it .And Finally works every API works fine.

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30
  • 1
    Kai, this solved it for me, the exception kept showing up for over a week, I just got to know that my emulator was not connected because it was launched before connecting my PC to the network – Peter Akwa Jul 01 '23 at 20:54
  • 1
    @PeterAkwa That is the main reason for this . – Rahul Kushwaha Jul 03 '23 at 04:44
5

If you are getting this error while using the flutter_socket_io plugin, don't forget to initialize the socket i.e socketIO.init(); socketIO.connect(); before attempting to subscribe to it. I received the same error as that indicated above when I omitted it.

Balocodes
  • 577
  • 7
  • 6
5

By enabling WIFI it works for me, and make sure you also added the internet permission in the android manifest it added automatically also but you have to make sure. if still not then invalidate caches and restart hope it works

Ahsan
  • 241
  • 4
  • 8
4

Make sure your Mobile device or Emulator is connected to the internet

4

The problem for me was that I was using a VPN. The emulator didn't have internet access when connected to the VPN on my computer. It might help someone.

Vincent Gagnon
  • 650
  • 2
  • 7
  • 15
3

When i enabled wifi on my device, it worked fine.

iheathers
  • 371
  • 6
  • 13
  • 3
    Please post your this type of answer in comment section if it contains code then its okay please make sure in future – Jaimil Patel Apr 21 '20 at 06:28
3

Even after adding

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

in your android/app/src/main/AndroidManifest.xml, if you're still finding trouble then

There maybe an error with the website's server IP address like:

Server IP address could not be found

It's better to wait for some time and make sure to restart wifi, it worked for me.

Pranay
  • 31
  • 4
3

Make sure your Emulator or your device connected to the internet. if your Emulator could not connect to the internet use a VPN.

3

I'm using arch Linux.

Linux arch 5.14.7-arch1-1

And Flutter version

Flutter 2.2.1 

Dart version

Dart 2.13.1

Code editor

1.58.0

In my case turning on wifi solved the problem.

islamux
  • 97
  • 5
2

Check if the wifi is connected or not. Mine worked after restarting the wifi.

Farwa
  • 6,156
  • 8
  • 31
  • 46
2
  • Make sure the internet is on (both on the phone and the PC)
  • also, make sure the server is working properly
  • check your URL properly
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
Nikita
  • 21
  • 1
2

Sometimes, other wifi can have interference with your connected wifi, which can cause this error as well. Try to set your connected wifi's channel to auto or away from other wifi's channels.

Ryan
  • 800
  • 1
  • 13
  • 27
2

I had the same problem while running the app on an emulator.

My laptop was connected to the internet through my phone's mobile hotspot. But apparently the emulator was unable to access the internet

Later, I switched to my Wi-Fi Router connection and the application proceeded smoothly as expected.

I'm not sure, but is there any special access to be granted?

Tip: Try googling something on the emulator to check internet connectivity

RoninS28
  • 43
  • 1
  • 8
2

For me it was just bad url with function "Uri.https(...)" in Flutter. Check if the address is correct with your browser

Florian K
  • 2,033
  • 1
  • 9
  • 21
2

The DNS settings on my mac were invalid...just use 8.8.8.8 without any internal DNS routing addresses

Mahmoud Hadad
  • 160
  • 3
  • 10
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 25 '22 at 23:08
2

Those who still can't manage to make it work Check if your internet permission and access network permissions are spelled correctly!
I had an extra space at the end of the permission and it didn't work only because of this!

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

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

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
1

The problem is basically coming when device is trying to access any internet resources,to fix add the to AndroidManifest.xml file @android/app/src location.

Sachin Mishra
  • 1,125
  • 1
  • 16
  • 17
1

Check if you have enabled offline work in the gradle settings. File-> Settings -> Build -> Build Tools -> Offline work

If its enabled or checked then uncheck it.

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
1

Also, might good to look at the 'Proxy settings' inside the 'Extended controls' of the emulator.

The way it worked for me was to change to use the 'No Proxy' setting.

enter image description here

Sukhi
  • 13,261
  • 7
  • 36
  • 53
  • For those who don't find this screen (*Extended controls*) in the new versions of *Android Studio*, you'll have to go to **Settings -> Emulator** and **uncheck Launch in a tool window**. Then restart your emualtor. – Philipos D. Jul 30 '21 at 08:45
1

Double-check / make sure you have

<uses-permission android:name="android.permission.INTERNET" /> in your app's android/app/src/main/AndroidManifest.xml

Riad Hossain
  • 371
  • 4
  • 5
1

if you're using vscode you would get gai_error=0 that's if the http is connected successfully

Here is the right place to place the internet permission because it was quite challenging to know the location

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appName">
<uses-permission android:name="android.permission.INTERNET" />

run the flutter run command on cmd or visual studio code terminal

1

I deliberately disconnected my emulator wifi to see what would happen (as there could be cases where no internet is available). The error led me here, but, all I see are workarounds and not error handling. Really, the app should be able to handle the error gracefully and not crash out because of a lack of connection.

So far, all I have done is to catch the error and if the return code is -1 (or anything other than a 200), I know I have a problem and can make the app respond appropriately.

var request = new http.MultipartRequest("POST", uri);

// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
    filename: fileToUpload.path);

// add file to multipart
request.files.add(multipartFile);

// send
try {
  var response = await request.send();
  print(response.statusCode);

  // listen for response
  response.stream.transform(utf8.decoder).listen((value) {
    print(value);
  });

  return response.statusCode;
}
catch (e)
{
  return -1;
}
David
  • 214
  • 3
  • 15
1

It's definitely internet issue as others have stated. @petro and @Tushar Nikam response put me in the right direction. My emulator was in airplane mode, but I don't know how; it's the one I use 90% of the time. I'd advise to check for anything that might block internet connectivity.

Hoxtygen
  • 106
  • 5
1

For me, the problem was that my emulator had network connections (LTE and Wi-Fi) disabled.

Guillem Puche
  • 1,199
  • 13
  • 16
1

I had similar problem the solution was to uncheck "all exception" inside debug dialog in vscode hopefully it helps someone with similar issue!

Rohan Arora
  • 303
  • 2
  • 12
0

Translated: In my case, the computer had internet, but the emulator did not. I configured the connection on the emulator and solved it.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 2
    @SkypeMeSM Please do not translate posts for the author. If the author cannot express their contribution in English, then they are also not able to take feedback, discuss and react. – Yunnosch Apr 30 '20 at 08:12
  • @Yunnosch The auther has give the right answer. And he has also translated their native language to English so that another author should learn and resolved this issues. – Rahul Kushwaha Dec 04 '21 at 09:30
  • @RahulKushwaha No, the author of this post did not translate it. – Yunnosch Dec 04 '21 at 09:50
0

check your url address , In my case I was using a wrong url.

0

make sure u are using Internet permission & your emulator has a wifi.

Mohmmaed-Amleh
  • 383
  • 2
  • 11
0

I had this error when called

  final currencies = await Forex().getAvailableCurrencies();

from forex_conversion package.

The error was appearing on the code which worked a minute ago.

flutter clean didn't help. Tried another android device - still got this error.

I resolved the issue by copying the project to another folder (with or without build folder - didn't matter). The error disappeared.

Andrey
  • 5,932
  • 3
  • 17
  • 35