flutternestjsdio

Flutter, Dio getting error while processing a request, error http 400


Sorry for the bad English, I wanted to test login api. I used NestJS + MongoDB. When I type email and password in and press, it gives me this error: HTTP error It said that data is null, but I'm sure that I used a textEditingController for it. And I did test if username and password is empty. But it still said that data was called on null Here are the codes:


auth_services.dart

import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:frontend/services/api_services.dart';

class AuthServicess {
  Dio dio = Dio(BaseOptions(connectTimeout: Duration(seconds: 5)));
  login(email, password) async {
    try {
      return await dio.post(
        'http://localhost:3000/users/login',
        data: {"email": email, "password": password},
      );
    } on DioException catch (e) {
      print(e.toString());
      Fluttertoast.showToast(msg: e.toString());
    }
  }
}

loginScreen.dart

FilledButton(
                  onPressed: () {
                    AuthServicess()
                        .login(username.text, password.text)
                        .then((value) {
                      if (value.data["status"]) {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => const HomeScreen()));
                      } else {
                        Fluttertoast.showToast(
                            msg: "Đăng nhập thất bại",
                            toastLength: Toast.LENGTH_SHORT,
                            gravity: ToastGravity.BOTTOM,
                            timeInSecForIosWeb: 1,
                            backgroundColor: Colors.red,
                            textColor: Colors.white,
                            fontSize: 16.0);
                      }
                    });
                  },
                  style: const ButtonStyle(
                    backgroundColor:
                        MaterialStatePropertyAll<Color>(Color(0xffFF810B)),
                  ),
                  child: const Text("Đăng nhập",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 20,
                          fontWeight: FontWeight.bold)),
                ),
              )```



I tried to fix the Options as someone on github recommended but it still doesn't work

Solution

  • Status code 400 (bad request) means the server side can't process your request, usually because client have sent wrong type, miss or null parameters.

    If you are sure you didn't send null or empty data to the server, please check your code on server side to see what data (and type of the data) was received from client, maybe the wrong type or error when parsing data type model so server can not read the parameters.