有没有什么好方法可以在Flutter中创建一个POST请求到JSON?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有没有什么好方法可以在Flutter中创建一个POST请求到JSON?相关的知识,希望对你有一定的参考价值。

所以我有一个Flutter应用程序,它从一个JSON中的api获取数据,并在listview.builder中显示。我现在想要发出POST请求并更改JSON中的数据。如果您知道如何做到这一点,请添加回复:)

JSON:

[
{
    "id": 29,
    "description": "Berlinmurens Fall",
    "state": "started",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        }
    ],
    "subtasks": []
},
{
    "id": 30,
    "description": "Kubakrisen + Konsekvenser",
    "state": "started",
    "assigned_users": [
        {
            "username": "studentone",
            "fullname": "Student One"
        }
    ],
    "subtasks": []
},
{
    "id": 31,
    "description": "Mutual Assured Destruction (MAD)",
    "state": "new",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        {
            "username": "studentone",
            "fullname": "Student One"
        },
        {
            "username": "studenttwo",
            "fullname": "Student Two"
        }
    ],
    "subtasks": [
        {
            "id": 3,
            "description": "Vad betyder MAD?"
        },
        {
            "id": 4,
            "description": "Vad har MAD att göra med Kalla Kriget?"
        },
        {
            "id": 5,
            "description": "Vem vann tillslut för att de fick kraften över MAD?"
        }
    ]
},
{
    "id": 32,
    "description": "Hur vann USA?",
    "state": "new",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        {
            "username": "studentone",
            "fullname": "Student One"
        }
    ],
    "subtasks": []
},
{
    "id": 33,
    "description": "Stilig Google Presentation",
    "state": "done",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        {
            "username": "studentone",
            "fullname": "Student One"
        },
        {
            "username": "studenttwo",
            "fullname": "Student Two"
        }
    ],
    "subtasks": []
},
{
    "id": 34,
    "description": "Vem var John F Kennedy?",
    "state": "done",
    "assigned_users": [
        {
            "username": "studentone",
            "fullname": "Student One"
        }
    ],
    "subtasks": []
}
]

我想改变“状态”。通过POST请求。有人可以告诉我该怎么做吗?

我应该这样做吗?

_postreq() async {

var url ="--ADRESS--";

var response = await http.post(
    url,
    headers:{ "Accept": "application/json" } ,
    body: { "state": 1}, //key value
    encoding: Encoding.getByName("utf-8")
);

print("post");

return response;
}

JSON与个人任务/ 29

GET /api/task/29/
HTTP 200 OK
Allow: GET, POST, PUT, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
"id": 29,
"description": "Berlinmurens Fall",
"state": "started",
"assigned_users": [
    {
        "username": "hugo",
        "fullname": "Hugo Johnsson"
    }
],
"subtasks": []
}

和UI

_postreq() async {

var url ="http://studieplaneraren.pythonanywhere.com/api/task/29/";

var response = await http.post(
    url,
    headers:{ "Accept": "application/json" } ,
    body: { "state": "done",}, //key value
    encoding: Encoding.getByName("utf-8")
);

print("post");

return response;
}

后端代码

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Mouldifi - A fully responsive, html5 based admin theme">
<meta name="keywords" content="Responsive, HTML5, admin theme, business, professional, jQuery, web design, CSS3, sass">
<title>{{projekt.titel}}</title>
答案

试试这个:

var url ="http://192.168.1.10/update.php";

//Metodo post
var response = await http.post(
    url,
    headers:{ "Accept": "application/json" } ,
    body: { "state": 1}, //key value
    encoding: Encoding.getByName("utf-8")
);

以上是关于有没有什么好方法可以在Flutter中创建一个POST请求到JSON?的主要内容,如果未能解决你的问题,请参考以下文章

如何在Flutter / Dart中创建异步回调?

在 Flutter 中创建图像轮播

有没有办法在 Flutter 的 TabBar 中创建下拉列表?

Flutter Cards:如何在 Flutter 中创建自定义卡片小部件

如何在 Flutter 中创建三角形 UI

如何在 Flutter 中创建服务以使应用始终在后台运行?