我用 Postman 嘗試了一個 API 端點,Put 方法作業得很好,但是在顫振中,它不會更新 textfromfield 中的欄位。嘗試了所有可能的解決方案,改變生活的幫助
void UpdateTask(int id, String a, String b) {
var updateUrl =
Uri.parse('https:........./api/update/${id}/');
setState(() {
widget.client.put(
updateUrl,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({
"Title": a,
"Desc": b,
}),
);
});
Navigator.pop(context);
}
import 'package:flutter/material.dart';
import 'package:http/http.dart';
class UpdatePage extends StatefulWidget {
final String title;
final String Desc;
final int idoftask;
final Client client;
const UpdatePage({
super.key,
required this.title,
required this.Desc,
required this.idoftask,
required this.client,
});
@override
State<UpdatePage> createState() => _UpdatePageState();
}
class _UpdatePageState extends State<UpdatePage> {
TextEditingController taskEditingController = TextEditingController();
TextEditingController desEditingController = TextEditingController();
@override
void initState() {
taskEditingController.text = widget.title;
desEditingController.text = widget.Desc;
super.initState();
}
void UpdateTask(int id, String a, String b) {
var updateUrl =
Uri.parse('https://-------/api/update/${id}/');
setState(() {
widget.client.put(
updateUrl,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({
"Title": a,
"Desc": b,
}),
);
});
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.yellow[200],
appBar: AppBar(
title: const Text(
'UPDATE THE TASK',
style: TextStyle(color: Colors.brown),
),
elevation: 0,
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 18, horizontal: 25),
child: Column(
children: [
TextFormField(
controller: taskEditingController,
decoration: InputDecoration(
hintText: 'Task',
hintStyle: TextStyle(color: Colors.grey),
),
),
SizedBox(height: 15),
TextFormField(
controller: desEditingController,
decoration: InputDecoration(
hintText: 'Description',
hintStyle: TextStyle(color: Colors.grey),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: (() => UpdateTask(
widget.idoftask,
taskEditingController.text,
desEditingController.text,
)),
child: Text('Edit '),
),
],
),
),
);
}
}
基于其他@Idriss 和其他 SOF 執行緒的更改。現在作業
Future<void> UpdateTask(int id, String a, String b) async {
var updateUrl =
Uri.parse('https://todobackenda.herokuapp.com/api/update/${id}/');
final response = await widget.client.put(
updateUrl,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({
"Title": a,
"Desc": b,
}),
);
if (response.statusCode == 200) {
print(response.body);
setState(() {});
}
Navigator.pop(context);
}
onPressed: (() async {
await UpdateTask(
widget.idoftask,
taskEditingController.text,
desEditingController.text,
);
}),`enter code here`
添加所有其他方法( GET. DELETE. CREATE . )有效,但 PUT .. aLSO put 方法在 Postman 中有效。aLSO,put 方法在 Postman 中有效

uj5u.com熱心網友回復:
您必須等待您的 api 呼叫,然后才能重新加載您的頁面。
所以你必須這樣做:
onPressed: (() async {
var response = await widget.client.put(
updateUrl,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({
"Title": a,
"Desc": b,
}),
);
if (response.statusCode == 200){
setState(() {
...
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/530967.html
標籤:扑镖heroku-api
上一篇:未處理的例外:NoSuchMethodError:類“FirebaseAuthException”沒有實體獲取器“_message”
