Flutter error: Unhandled Exception: HandshakeException: Handshake error in client

996 views Asked by At

I'm trying to test my flutter app locally, connecting to my API locally. I have that application running on localhost:5000, and this is the code of a simple login form I've created.

import 'dart:convert';

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

import 'groups.dart';
import 'signup.dart';

class Token {
  final String userToken;

  Token({this.userToken});

  factory Token.fromJson(Map<String, dynamic> json) {
    print(json);
    return Token(
      userToken: json['title'],
    );
  }
}

class Login extends StatefulWidget {
  Login({Key key}) : super(key: key);

  @override
  LoginState createState() => LoginState();
}

class LoginState extends State<Login> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  var userData = {};

  void updateField(String value, String field) {
    setState(() {
      userData = {
        ...userData,
        '$field': value,
      };
    });
  }

  Future<http.Response> sendLogin(user) {
    final response = http.post(
        Uri.parse('https://10.0.2.2:5000/api/User/Login'),
        headers: <String, String>{
          'Content-Type': 'application/json',
        },
        body: jsonEncode(user),
    );
    print(response);
    return response;
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Column(children: <Widget>[
        Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              TextFormField(), // these 2 form fields are commented out because they're already confirmed
              TextFormField(), // to be working perfectly.
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 16.0),
                child: ElevatedButton(
                  onPressed: () {
                    if (_formKey.currentState.validate()) {
                      sendLogin(userData);
                      Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) => Groups(),
                      ));
                    }
                  },
                  child: Text('Submit'),
                ),
              ),
            ],
          ),
        ),
        ElevatedButton(), // removed inside because it's a link to another page
    );
  }
}

I've looked at this link here how to solve flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?

That link led me to include more in my main.dart file, at least for testing purposes, ending that like this

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:mobile_v2/Packages/user-check.dart';

void main() {
  HttpOverrides.global = new MyHttpOverrides();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Gathering',
      home: UserCheck(),
    );
  }
}

class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }
}

After making these changes, I ran a flutter pub get, just to be safe, and yet I'm still having the same issues. I've looked through most of the answers in that post, and it still gives the same result. Any other possible answers would be appreciated. Thank you. (flutter doctor below as well)

[√] Flutter (Channel stable, 2.0.5, on Microsoft Windows [Version 10.0.19041.985], locale en-US)
    • Flutter version 2.0.5 at C:\src\flutter\flutter
    • Framework revision adc687823a (5 weeks ago), 2021-04-16 09:40:20 -0700
    • Engine revision b09f014e96
    • Dart version 2.12.3

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\ocdam\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] IntelliJ IDEA Community Edition (version 2019.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart

[√] VS Code (version 1.56.2)
    • VS Code at C:\Users\ocdam\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.22.0

[√] Connected device (3 available)
    • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 90.0.4430.212
    • Edge (web)                  • edge          • web-javascript • Microsoft Edge 90.0.818.49

• No issues found!
0

There are 0 answers