1

我正在尝试在本地测试我的颤振应用程序,在本地连接到我的 API。我在 localhost:5000 上运行了该应用程序,这是我创建的一个简单登录表单的代码。

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
    );
  }
}

我在这里查看了这个链接,如何在执行 POST 请求时解决颤振 CERTIFICATE_VERIFY_FAILED 错误?

该链接使我在 main.dart 文件中包含更多内容,至少出于测试目的,以这样的方式结束

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;
  }
}

进行这些更改后,为了安全起见,我运行了一个颤振的 pub get,但我仍然遇到同样的问题。我已经浏览了该帖子中的大部分答案,但仍然给出了相同的结果。任何其他可能的答案将不胜感激。谢谢你。(下面还有颤振医生)

[√] 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!
4

0 回答 0