Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making Network http error SocketException: Failed host lookup #27883

Closed
hoc081098 opened this issue Feb 13, 2019 · 34 comments
Closed

Making Network http error SocketException: Failed host lookup #27883

hoc081098 opened this issue Feb 13, 2019 · 34 comments
Labels
c: crash Stack traces logged to the console dependency: dart Dart team may need to help us engine flutter/engine repository. See also e: labels.

Comments

@hoc081098
Copy link

hoc081098 commented Feb 13, 2019

Steps to Reproduce

Making Network http error when build release, even having internet connection
When build debug having no error

Error: SocketException: Failed host lookup: 'jsonplaceholder.typicode' (OS  Error No address associated with hostname, error = 7)

Code:

main.dart:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.dark(),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const url = 'https://jsonplaceholder.typicode.com/users';

  Future<List<Map<String, dynamic>>> _future;

  @override
  void initState() {
    super.initState();
    _future = fetch();
  }

  Future<List<Map<String, dynamic>>> fetch() {
    return http
        .get(url)
        .then((response) {
          return response.statusCode == 200
              ? response.body
              : throw 'Error when getting data';
        })
        .then((body) => json.decode(body))
        .then((list) => (list as List).cast<Map<String, dynamic>>());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: RefreshIndicator(
        onRefresh: () async {
          _future = fetch();
          setState(() {});
          return _future;
        },
        child: FutureBuilder<List<Map<String, dynamic>>>(
          future: _future,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Center(
                child: Container(
                  constraints: BoxConstraints.expand(),
                  child: SingleChildScrollView(
                    physics: AlwaysScrollableScrollPhysics(),
                    child: Text(snapshot.error.toString()),
                  ),
                ),
              );
            }

            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            return ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                final item = snapshot.data[index];
                return ListTile(
                  title: Text(item['name']),
                  subtitle: Text(item['email']),
                );
              },
            );
          },
        ),
      ),
    );
  }
}

Screenshots:

Lenovo a2010: android 5.1

Philips S337: android 5.1:

Logs

flutter analyze

Analyzing test_http...
No issues found! (ran in 3.5s)

flutter doctor -v

[√] Flutter (Channel dev, v1.2.0, on Microsoft Windows [Version 10.0.17134.523], locale en-US)
    • Flutter version 1.2.0 at C:\flutter\flutter
    • Framework revision 06b979c4d5 (3 weeks ago), 2019-01-25 14:27:35 -0500
    • Engine revision 36acd02c94
    • Dart version 2.1.1 (build 2.1.1-dev.3.2 f4afaee422)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at C:\Users\Admin\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: E:\Admin\Other\android-preview\android-studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[!] Android Studio (version 3.3)
    • Android Studio at E:\Admin\Other\android-preview\android-studio
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[√] IntelliJ IDEA Community Edition (version 2018.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3
    • Flutter plugin version 32.0.3
    • Dart plugin version 183.5153.38

[√] VS Code (version 1.31.0)
    • VS Code at C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 2.22.3

[√] Connected device (1 available)
    • Philips S337 • FS011641B06736 • android-arm • Android 5.1 (API 22)
@zoechi
Copy link
Contributor

zoechi commented Feb 13, 2019

Are you using a proxy?

@zoechi zoechi added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Feb 13, 2019
@hoc081098
Copy link
Author

No. I don't use proxy

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Feb 15, 2019
@zoechi
Copy link
Contributor

zoechi commented Feb 15, 2019

Hard to tell. Looks like a network issue.
Do other remote or local network adresses work?

@zoechi zoechi added waiting for confirmation before landing waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds and removed waiting for confirmation before landing labels Feb 15, 2019
@hoc081098
Copy link
Author

I tried many network but still not working

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Feb 19, 2019
@zoechi
Copy link
Contributor

zoechi commented Feb 20, 2019

Shouldn't this be jsonplaceholder.typicode.com instead of jsonplaceholder.typicode?

@zoechi
Copy link
Contributor

zoechi commented Feb 20, 2019

Please consider asking support questions in one of the other channels listed at http://flutter.io/support .

@zoechi zoechi closed this as completed Feb 20, 2019
@hoc081098
Copy link
Author

i think that url is not a problem.
paste url https://jsonplaceholder.typicode.com/users in browser working well
static const url = 'https://jsonplaceholder.typicode.com/users'; in class _MyHomePageState

@zoechi zoechi reopened this Feb 20, 2019
@zoechi
Copy link
Contributor

zoechi commented Feb 20, 2019

@mraleph could this be a tree-shaking issue or something like that?
(sorry, don't know whom else to summon)

@zoechi zoechi added engine flutter/engine repository. See also e: labels. dependency: dart Dart team may need to help us labels Feb 20, 2019
@zoechi zoechi added this to the Goals milestone Feb 20, 2019
@zoechi zoechi added the c: crash Stack traces logged to the console label Feb 20, 2019
@hoc081098
Copy link
Author

i rebuild release app and this is error:

E/flutter (11529): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
E/flutter (11529):      BAD_SIGNATURE(ecdsa.c:247)
E/flutter (11529):      BAD_SIGNATURE(handshake_client.cc:1060))
E/flutter (11529): #0      IOClient.send (package:http/src/io_client.dart:33:23)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #2      BaseClient.get (package:http/src/base_client.dart:32:7)
E/flutter (11529): #3      get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter (11529): #4      _withClient (package:http/http.dart:166:20)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #5      get (package:http/http.dart:46:5)
E/flutter (11529): #6      _MyHomePageState.fetch (package:test_http/main.dart:37:12)
E/flutter (11529): #7      _MyHomePageState.build.<anonymous closure> (package:test_http/main.dart:56:21)
E/flutter (11529): <asynchronous suspension>
E/flutter (11529): #8      RefreshIndicatorState._show.<anonymous closure> (package:flutter/src/material/refresh_indicator.dart:357:53)
E/flutter (11529): #9      _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter (11529): #10     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (11529): #11     _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter (11529): #12     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)

E/flutter (11529): #13     Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter (11529): #14     Future._completeWithValue (dart:async/future_impl.dart:483:5)
E/flutter (11529): #15     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:513:7)
E/flutter (11529): #16     _rootRun (dart:async/zone.dart:1124:13)
E/flutter (11529): #17     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (11529): #18     _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter (11529): #19     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
E/flutter (11529): #20     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (11529): #21     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (11529):

@zarko-tg
Copy link

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

@hoc081098
Copy link
Author

hoc081098 commented Mar 12, 2019

Thank @zarko-tg.

Solve: add <uses-permission android:name="android.permission.INTERNET" /> to android/app/src/main/AndroidManifest.xml

@ferso
Copy link

ferso commented Apr 26, 2019

Thank @zarko-tg.

Solve: add <uses-permission android:name="android.permission.INTERNET" /> to android/app/src/main/AndroidManifest.xml

This saves me, Why this is not in the documentation ?

@zarko-tg
Copy link

@ferso It'd be less to worry about or consult documentation if they'd make it a default (in templates/starters) since most apps connect online. I spent like a day on a silly little thing.

@xai1983kbu
Copy link

xai1983kbu commented Apr 30, 2019

I had that error even in Debug mode (android/app/src/debug/AndroidManifest.xml has
<uses-permission android:name="android.permission.INTERNET" />)

Adding internet permission is not only a solution,
You also have to make sure that you are online whether it is mobile or emulator (c)
https://stackoverflow.com/a/55548864/9783262

I wiped data
C:\Users\UN\AppData\Local\Android\Sdk\emulator> .\emulator -avd Nexus_5X_API_28 -wipe-data
(or via Android Studio)
and run main.dart

It works!
p.s. it's not related to release but I hope it will save time to someone.

@RobertB4
Copy link

RobertB4 commented May 11, 2019

Adding <uses-permission android:name="android.permission.INTERNET" /> solved this issue for me.

The documentation is out of date and should be updated. Since Flutter uses the internet permission internally in development mode it is always active when developing. When deploying the app to the play store, it is not active by default.

The documentation currently says:

uses-permission: Remove the android.permission.INTERNET permission if your application code does not need Internet access. The standard template includes this tag to enable communication between Flutter tools and a running app.

Which is misleading since the internet permission isn't in the production manifest (apparently it was in earlier flutter versions?). Please add a note to this so the documentation accurately explains that the internet permission has to be added to the production manifest in case it is needed.

@atapas
Copy link

atapas commented May 25, 2019

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

Great Info. This helped me.

As I was wondering a bit that, where to add this line in that file... figured it out to be at the end like:

image

@alvinkonda
Copy link

This line used to be there on default on previous version of Flutter. It seems that they have removed on the recent updates.

@abhisawesome
Copy link

Make sure the debugging device internet is turned on , if you use localhost try changing it into 10.0.2.2

@Emyboy
Copy link

Emyboy commented Sep 8, 2019

Thank @zarko-tg.

Solve: add <uses-permission android:name="android.permission.INTERNET" /> to android/app/src/main/AndroidManifest.xml

would I have to restart the app before it works? Mine isn't working.

@zarko-tg
Copy link

zarko-tg commented Sep 8, 2019

@Emyboy yes, it means building and starting the app again. If it isn't working that your are likely having some other problem, unrelated to this Github issue.

@Abdusin
Copy link

Abdusin commented Oct 23, 2019

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

@zarko-tg pls help me this way dont solve my problem
flutter/flutter/#43357

@tonysader
Copy link

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

@zarko-tg Thanks man that saved me too!

@SuperManHa
Copy link

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

great! works for me!!

@bismarkstoney
Copy link

After trying all the steps. if You are running the server locally. In my case Strapi. Replace the localhost with http://localhost:1337/auth/local/register to http://10.0.2.2:1337/auth/local/register

@ph55
Copy link

ph55 commented Jan 18, 2020

I get this error on IOS.

SocketException: Failed host lookup: 'locallhost'

The solution listed here is for Android though.
Anyone managed to fix it for iPhone simulator ?

@Purvik
Copy link

Purvik commented Jan 23, 2020

@ph55 Hey, having same issue on iOS. Did you manage to solve it?

@cahyowhy
Copy link

cahyowhy commented Feb 3, 2020

already set permission but still got that err

environment:
  sdk: ">=2.3.0 <3.0.0"
  ....
..
flutter_svg: ^0.14.0

@xRose1
Copy link

xRose1 commented Feb 4, 2020

I cant display my api pictures due to this

@ericchuawc
Copy link

i tried and can't work too..

then realised my android simulator wifi is off... need to enable it ya :)

@sumonhub
Copy link

sumonhub commented Apr 2, 2020

make sure your device has internet connection.

@akadatsky
Copy link

I have this issue on release build, but debug build works ok.
Manual adding
works, but its waird why it works on debug build

@enasellithy
Copy link

@hoc081098 Double-check / make sure you have
<uses-permission android:name="android.permission.INTERNET" />
in your app's android/app/src/main/AndroidManifest.xml

please help my i have same isuee and i try your way not working with me i use this this endpoint
https://yts.mx/api/v2/list_movies.json
Screenshot from 2020-04-10 17-21-51
Screenshot from 2020-04-10 17-21-38

@lock
Copy link

lock bot commented Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@lock lock bot locked and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: crash Stack traces logged to the console dependency: dart Dart team may need to help us engine flutter/engine repository. See also e: labels.
Projects
None yet
Development

No branches or pull requests