我现在正在Android Studio中测试这个.这是我在Flutter前端中try 使用的Go代码:

func Login(c *gin.Context) {
    //  Get email and password off of req body
    var body struct {
        Email    string
        Password string
    }

    if c.Bind(&body) != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "error": "Failed to read body",
        })

        return
    }

    var user models.User
    if user.ID == 0 {
        c.JSON(http.StatusBadRequest, gin.H{
            "error": "Invalid email and/or password",
        })
        return
    }

    // Lookup user by email, including records where deleted_at is NULL
    if err := initializers.DB.Where("email = ? AND deleted_at IS NULL", body.Email).First(&user).Error; err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "error": "Invalid email and/or password",
        })
        return
    }

    //  Compare passwords
    err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(body.Password))
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "error": "Invalid email and/or password",
        })
        return
    }

    //  Generate JWT Token
    token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
        "sub": user.ID,
        "exp": time.Now().Add(time.Hour * 24 * 30).Unix(),
    })
    tokenString, err := token.SignedString([]byte(os.Getenv("SECRET")))

    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "error": "Failed to create token",
        })
        return
    }

    //  Send back
    c.SetSameSite(http.SameSiteLaxMode)
    c.SetCookie("AuthZ", tokenString, 3600*24*30, "", "", false, true)

    c.JSON(http.StatusOK, gin.H{})
}

//main,go

    r.POST("/login", controllers.Login)

我一直收到400错误代码,我正在try 解析原因.以下是我发送请求的方式:

void postLogin(String email, String password) async {
  Response response;
  response = await _dio.post('$baseUrl/login', data: {
    'email': email,
    'password': password,
  });
}

...

try {
                          postLogin(
                              emailController.text, passwordController.text);
                          print('Login successful');
                        } catch (error) {
                          print('Login failed: $error');
                          // Print Dio error message
                          if (error is DioError) {
                            print('Dio error message: ${error.message}');
                          }
                          setState(() {
                            errorText =
                                'Login failed. Please check your credentials.';
                          });
                        } finally {
                          setState(() {
                            isLoading = false;
                          });
                        }

该API在Postman和其他测试方法中都可以使用,但在Android Studio中不能使用,至少在我使用Dio的方式下不能使用.

谢谢你的帮助.

推荐答案

这看起来像是你的问题:

var user models.User
if user.ID == 0 {
    c.JSON(http.StatusBadRequest, gin.H{
        "error": "Invalid email and/or password",
    })
    return
}

假设models.User上的ID只是int,默认是is always zero!在判断ID之前,您可能希望从数据库加载用户.

Flutter相关问答推荐

想更新ListView上的索引点击块Flutter

Build.gradle专线:2个Flutter Flutter

如何将 colored颜色 阴影作为默认容器 colored颜色 应用于自定义窗口小工具?

Flutter :执行com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction时出现故障

如何确定Flutter 中的文本 colored颜色 ?

在Flutter 小部件测试中找不到框中的AssetImage装饰

使用持久的侧边菜单和顶部栏在Ffltter Web应用程序中的页面之间导航

抖动问题:检测到0个或2个或更多具有相同值的[DropdownMenuItem]

Flutter中的编译时间常量方法

在 Flutter 中从 FireStore 获取数据并编译它们需要太多时间

当我使用 Confetti Widget 时,Flutter Android 和 IOS 应用程序崩溃

如何修复 ICU Lexing 错误:Flutter 中的意外字符

对话框打开时如何使背景模糊?

Flutter HLS .m3u8 视频无法实时播放

无法在 flutter 中更新 void 方法内的变量值

无法在Flutter 图像小部件中打开大尺寸(500MB)图像

更新未知文档 ID firebase\flutter 中的字段

在 FAB 上添加表格小部件单击 FLUTTER

一个或多个插件需要更高的 Android SDK 版本

有没有办法帮助我修复类型 null 不是 Map 类型的子类型?