gpt4 book ai didi

Flutter Http.put 更新来自 api 的数据

转载 作者:行者123 更新时间:2023-12-05 05:48:07 25 4
gpt4 key购买 nike

我有一个屏幕,一个包含所有用户信息的用户个人资料屏幕,在同一个屏幕上,我有一个按钮,当按下它时,它会发送到编辑个人资料屏幕,用户可以在其中更改他的帐户信息,我从中获取帐户信息api,我正在尝试使用 http.put 所以当用户写一些东西来更新他的名字和姓氏时,当用户按下保存更改按钮时,我希望数据更新为用户写的内容,并在配置文件屏幕中显示数据应该更新。我收到 415 错误消息。

final editedFirstName = TextEditingController();
final editedLastName = TextEditingController();

body: FutureBuilder<Response>(
future: futureData,
builder: (context, snapshot) {
if (snapshot.hasData) {
AccountData data3 = AccountData.fromJson(
json.decode(snapshot.data!.body),
);
updatedFirstName = data3.firstName;
updatedLastName = data3.lastName;

child: TextField(
controller: editedFirstName,
//initialValue: updatedFirstName,
decoration: InputDecoration(
border: OutlineInputBorder(),
),
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.bold),
inputFormatters: [
LengthLimitingTextInputFormatter(15)
],
),

child: TextField(
controller: editedLastName,
//initialValue: updatedLastName,
decoration: InputDecoration(
border: OutlineInputBorder(),
),
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.bold),
inputFormatters: [
LengthLimitingTextInputFormatter(15)
],
),



late Future<Response> futureData;
String? updatedFirstName;
String? updatedLastName;

final editedFirstName = TextEditingController();
final editedLastName = TextEditingController();

Future<void> putAccountData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? authorization = prefs.getString('authorization');
var url = 'https://dev.api.wurk.skyver.co/api/v1/employees/account';
final Map payload = {
"firstName": editedFirstName.text,
"lastName": editedLastName.text
};
try {
final response = await http.put(Uri.parse(url),
headers: <String, String>{
'authorization': authorization ?? basicAuth.toString()
},
body: json.encode(payload));
print(response.body);
} catch (er) {
print(er);
}
}

ElevatedButton( // button to update changes and navigate to the
profile screen with the updated data
child: const Text(
'Save Changes ✓',
style: TextStyle(fontSize: 18),
),
onPressed: () {
print(editedFirstName.text);
print(editedLastName.text);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const ProfileScreen(),
),
);
},
),



最佳答案

试试这个:

ElevatedButton(
child: const Text(
'Save Changes ✓',
style: TextStyle(fontSize: 18),
),
onPressed: () async {
await putAccountData();
print(editedFirstName.text);
print(editedLastName.text);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const ProfileScreen()),
);
},
)

putAccountData 放在构建方法之外,但放在类中,如下所示:

      Future<void> putAccountData() async {
String url = 'some url';
final Map payload = {
"firstName": editedFirstName.text,
"lastName": editedLastName.text
};
try {
final response = await http.put(Uri.parse(url),
body: jsonEncode(payload));
print(response.body);
} catch (er) {}
}

关于Flutter Http.put 更新来自 api 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70865892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com