1

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):

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Rynia
  • 13
  • 1
  • 3
  • 1
    Does this answer your question? [Flutter Insecure http is not allowed by platform](https://stackoverflow.com/questions/64172791/flutter-insecure-http-is-not-allowed-by-platform) – Midhun MP May 21 '21 at 17:25
  • Are you on Android Emulator? If yes, change `http://localhost:3000/api/users/session` by `http://10.0.2.2:3000/api/users/session` – BLKKKBVSIK May 21 '21 at 17:31
  • Hello, yes, i'm on Android Emulator. I'm have tried with http://10.0.2.2:3000/api/users/session but this don't work. With https://10.0.2.2:3000/api/users/session and this don't work too :/ – Rynia May 21 '21 at 18:08

1 Answers1

1

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">
Coder_Manuel
  • 22
  • 1
  • 5