Flutter Http.put 更新来自 api 的数据
Flutter Http.put to update data coming from api
我有一个屏幕,一个包含所有用户信息的用户个人资料屏幕,在同一个屏幕上,我有一个按钮,当按下它时,它会发送到编辑个人资料屏幕,用户可以在其中更改他的帐户信息,我得到了帐户来自 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
放在构建方法之外,但放在 class 中,如下所示:
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) {}
}
我有一个屏幕,一个包含所有用户信息的用户个人资料屏幕,在同一个屏幕上,我有一个按钮,当按下它时,它会发送到编辑个人资料屏幕,用户可以在其中更改他的帐户信息,我得到了帐户来自 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
放在构建方法之外,但放在 class 中,如下所示:
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) {}
}