因此,我有一个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;
}
带有单独任务/ 29的JSON
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": []
}
和用户界面
_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>
答案 0 :(得分: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")
);