关于Flask后端出现401报错的问题

我是用flutter前端来访问flask后端的API,结果出现了401报错,是不是少了个什么授权的东西,要如何解决
报错

img

这是我的后端代码

from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS
import hashlib
import pymysql

app = Flask(__name__)
api = Api(app)
CORS(app)

conn = pymysql.connect(host='localhost', user='root', password='123456789', db='python_mysqlclient_test', charset='utf8mb4',
                       cursorclass=pymysql.cursors.DictCursor)


class Register(Resource):
    def post(self):
        data = request.json
        username = data.get('username')
        password = data.get('password')
        hash_password = hashlib.sha256(password.encode()).hexdigest()

        cursor = conn.cursor()
        cursor.execute('SELECT * FROM app01_userinfo WHERE username=%s', (username,))
        result = cursor.fetchone()

        if result:
            return {'message': 'User already exists.'}, 400

        cursor.execute('INSERT INTO app01_userinfo (username, password) VALUES (%s, %s)', (username, hash_password))
        conn.commit()

        return {'message': 'Registration successful.'}, 201


class Login(Resource):
    def post(self):
        data = request.json
        username = data.get('username')
        password = data.get('password')
        hash_password = hashlib.sha256(password.encode()).hexdigest()

        cursor = conn.cursor()
        cursor.execute('SELECT * FROM app01_userinfo WHERE username=%s AND password=%s', (username, hash_password))
        result = cursor.fetchone()

        if not result:
            return {'message': 'Invalid credentials.'}, 401

        return {'message': 'Login successful.'}, 200,print("login成功")


api.add_resource(Register, '/register')
api.add_resource(Login, '/login')

if __name__ == '__main__':
    app.run(host="0.0.0.0",debug=True)

以下是flutter前端代码


import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class Myapp extends StatefulWidget {
  const Myapp({Key? key}) : super(key: key);

  @override
  State<Myapp> createState() => _MyappState();
}

class _MyappState extends State<Myapp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'test',
      home: Scaffold(
        body: LoginScreen(),
    ),
    );
  }
}


class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  final _formKey = GlobalKey<FormState>();
  final _usernameController = TextEditingController();
  final _passwordController = TextEditingController();

  Future<void> _login() async {
    final username = _usernameController.text;
    final password = _passwordController.text;

    try {
      final response = await Dio().post(
        'http://192.168.2.7:5000/login',
        data: {'username': username, 'password': password},
      );

      // handle successful login
    } catch (e) {
      // handle login failure
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Login'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              TextFormField(
                controller: _usernameController,
                decoration: InputDecoration(
                  labelText: 'Username',
                ),
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Please enter your username';
                  }
                  return null;
                },
              ),
              TextFormField(
                controller: _passwordController,
                decoration: InputDecoration(
                  labelText: 'Password',
                ),
                obscureText: true,
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Please enter your password';
                  }
                  return null;
                },
              ),
              SizedBox(height: 16.0),
              ElevatedButton(
                child: Text('Login'),
                onPressed: () {
                  if (_formKey.currentState!.validate()) {
                    _login();
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

void main(){
  runApp(Myapp());
}