简体   繁体   中英

How to pass this 'Dio Post Request' Problem in Flutter

I have to send an object from the request. Although it works on Swagger, I cannot send it from my flutter code. When I was trying it, it always gave me Http status error 400 This problem is coming my wrong request. I examined the code of the Internet. I thought I implemented it correctly but It still does not work. Please, could you help me to solve it? Here is my interested code areas:

my api side =>

Future changePassword(id, password) async {
  try {
    var prefs = await SharedPreferences.getInstance();
    // User sendingData = User(
    //     id: id,
    // );
    // final dataSend = sendingData.toJson();
    final response = await dio.post('http://192.168.1.108:2308/api/Account/ChangePassword',
        data: jsonEncode({
          "id": "$id",
          "password": "$password"
        }),
        options: Options(headers: {
          HttpHeaders.acceptHeader: '*/*',
          HttpHeaders.contentTypeHeader: 'application/json',
        }));
    if (response.statusCode != 200) {
      // debugPrint('burası-------------------------' + response.statusCode.toString());
    }
    prefs.remove('password');
    return response;
  }
  catch (e) {
    debugPrint(e.toString());
  }
}

this is my screen side that I called it =>

onPressed: () async {
  var prefs = await SharedPreferences.getInstance();
  var pwd = prefs.getString('password');
  var email = prefs.getString('email');
  final usrId = prefs.getInt('userId');
  pwdFormKey.currentState!.save();
  if (pwdFormKey.currentState!
      .validate()) {
    try {
      if(oldPwdController.text==pwd){
        if(newPwdController.text==newPwdController2.text) {
          await api.changePassword(usrId, newPwdController.text);
              Navigator.pop(context);
              Fluttertoast.showToast(
                  msg: "Şifreniz başarıyla değiştirilmiştir.",
                  toastLength: Toast
                      .LENGTH_SHORT,
                  gravity: ToastGravity
                      .BOTTOM,
                  backgroundColor: CupertinoColors
                      .activeGreen,
                  textColor: CupertinoColors
                      .black,
                  fontSize: 16.0);
            }
          // }
        // }
           else {
          Fluttertoast.showToast(
              msg: "Yeni girdiğiniz şifreler aynı değil, lütfen kontrol ediniz.",
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.BOTTOM,
              backgroundColor: CupertinoColors.systemRed,
              textColor: CupertinoColors.black,
              fontSize: 16.0);
        }
      }
      else{
        Fluttertoast.showToast(
            msg: "Eski Şifreniz Hatalıdır.",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            backgroundColor: CupertinoColors.systemRed,
            textColor: CupertinoColors.black,
            fontSize: 16.0);
      }
    } catch (e) {
      return showDialog(
          context: context,
          builder: (BuildContext
          context) {
            return AlertDialog(
              title: Text(
                  "Hatalı Deneme"),
              content:
              SingleChildScrollView(
                child: ListBody(
                  children: [
                    Text(
                        "Hatalı şifre değiştirme işleminde bulundunuz. Lütfen tekrar deneyiniz.")
                  ],
                ),
              ),
              actions: [
                TextButton(
                  child: const Text(
                      'Tamam'),
                  onPressed: () {
                    Navigator.of(
                        context)
                        .pop();
                  },
                ),
              ],
            );
          });
    }
  }
},

In post method there are 2 diff values. One is data and another one is queryParameters . You can try in this way.

Map<String, dynamic>? param = {"userName": "name"}; 

return await (_dio.post(
        "http://192.168.1.108:2308/api/Account/ChangePassword",
        data: data, //Optional
        queryParameters: params,
      )).catchError((e) {
        if (!checkSessionExpire(e, context)) {
          throw e;
        }
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM