본문 바로가기
Flutter/Flutter FAQ

Flutter 플러터에서 Dart http 패키지를 사용하여 localhost:8000을 가리키는 방법은 어떻게 되나요?, How to point to localhost:8000 with the Dart http package in Flutter?

by 베타코드 2023. 8. 11.
반응형

질문


I'm following the Flutter Networking/HTTP tutorial to do a GET request to a server running on my localhost:8000. Visiting my localhost via my browser works fine. My code looks like this:

var url = 'http://localhost:8000';
Future<String> getUnits(String category) async {
    var response = await httpClient.get('$url/$category');
    return response.body;
}

This works fine when I point to any real URL, such as https://example.com, but when I point to https://localhost:8000 or https://localhost (or any variations of these), I get an error starting with:

E/flutter ( 4879): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 4879): SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 47060
E/flutter ( 4879): #0      IOClient.send (package:http/src/io_client.dart:30:23)

The port in the error above changes each time I reload the app. I looked in the http package code and it doesn't seem like there is a way to specify the port for the URL. How do I point to my localhost?


답변


문자열 localhost10.0.2.2로 대체하면 문제가 해결되었습니다. 저는 안드로이드 에뮬레이터에서 코드를 실행하고 있었는데, 이는 가상 머신에서 실행되고 있습니다. 이는 사실상 이 질문의 중복입니다.

반응형

댓글