如何从dart/flutter将int/integer值发布为json [英] How to post int/integer value as json from dart/flutter

查看:529
本文介绍了如何从dart/flutter将int/integer值发布为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net核心Web API作为后端.有一种方法可以接受单个整数值.

I am using asp.net core web API as back-end. There is a method that accepts a single integer value.

Method([FromBody] int value)

我想发布来自dart/flutter的整数值. 我用dart http包尝试了以下方法.

I want to post the integer value from dart/flutter. I tried the following with the dart http package.

Http.post(url, body:0,headers:{"content-type":"application/json"}

Http.post(url, body:{0},headers:{"content-type":"application/json"}

Http.post(url, body:convert.jsonEncode(0),headers:{"content-type":"application/json"}

Http.post(url, body:convert.jsonEncode({0}),headers:{"content-type":"application/json"}

我上面的所有尝试均因错误而失败 无效的参数:无效的请求正文"0""

All my above tries failed with error "Invalid argument: invalid request body "0""

推荐答案

请参考我的代码

  1. 导入

  1. import

import 'package:http/http.dart' as http;

  • http请求

  • http request

    var client = new http.Client();
    client.post(Uri.encodeFull("Your url"), body: {
      "param1": "value1",
      "param2": 11, // integer value type       
    }).then((response) {
      client.close();
      if (this.mounted && response.statusCode == 200) {
         //enter your code for change state
      }
    }).catchError((onError) {
       client.close();
       print("Error: $onError");
    });
    

  • 希望它能对您有所帮助.

    I hope it will help you.

    PS:

    var client = new http.Client();
    var response = await client.post(Uri.encodeFull("Your Url"), body : "0", header : {/*Your headers*/"});
    

    这篇关于如何从dart/flutter将int/integer值发布为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆