Rynia
Rynia

Reputation: 13

How to connect to Localhost?

I'm here new, I'm learning flutter and I need login with this:
POST http://localhost:3000/api/users/session

But I still can't login :( I'm waiting 5 minutes and still there is a loading screen with no effects.

I have try

So I have have made a this api_service with this tutorial made https://www.youtube.com/watch?v=_Kw4BfNX1-4

here is my api_service.dart

import 'package:http/http.dart' as http;
import 'dart:convert';
import '../model/login_model.dart';

class APIService {
Future<LoginResponseModel> 
login(LoginRequestModel requestModel) async {
String url = "http://localhost:3000/api/users/session";

final response = await http.post(url, body: requestModel.toJson());
if (response.statusCode == 200 || response.statusCode == 400) {
  return LoginResponseModel.fromJson(
    json.decode(response.body),
  );
} else {
  throw Exception('Failed to load data!');
}
}
}

Here is debug console:

E/flutter (10967): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 58856 E/flutter (10967):

I'm try with this page: https://medium.com/@podcoder/connecting-flutter-application-to-localhost-a1022df63130

I change String url = "http://localhost:3000/api/users/session"; to String url = "http://10.0.2.2:3000/api/users/session";

in my debug console i have completely different messages, but stilll don't work :(

E/flutter (10967):

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)]

Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform: http://10.0.2.2:3000/api/users/session

E/flutter (10967): #0 _HttpClient._openUrl (dart:_http/http_impl.dart:2434:7)

E/flutter (10967): #1 _HttpClient.openUrl (dart:_http/http_impl.dart:2341:7)

E/flutter (10967): #2 IOClient.send package:http/src/io_client.dart:31

(...) to #28 and the last ist E/flutter (10967):

Upvotes: 1

Views: 3992

Answers (1)

Coder_Manuel
Coder_Manuel

Reputation: 22

Fix for the Error Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform

Flutter terms http as an insecure source Therefore you should either use https or set android:usesCleartextTraffic to true in AndroidManifest

<uses-permission android:name="android.permission.INTERNET" />
        <application
                android:name="io.flutter.app.FlutterApplication"
                android:label="receipt"
                android:usesCleartextTraffic="true" // Add this line
                android:icon="@mipmap/ic_launcher">

Upvotes: 1

Related Questions