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

问题描述 投票:0回答:1

所以我有一个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>
json post flutter request http-post
1个回答
0
投票

试试这个:

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")
);
© www.soinside.com 2019 - 2024. All rights reserved.