Reputation: 294
I have a screen a user profile screen with all the user info, in that same screen i have a button that when presses it will send to edit profile screen where the user can change his account info, i get the account info from the api, im trying to use http.put so when the user writes something to update his name and surname and when the user presses the save changes button i want the data to update to what the user wrote, and in the profile screen the data should be updated. Im getting a 415 error message.
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(),
),
);
},
),
Upvotes: 3
Views: 3670
Reputation: 12673
Try this:
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()),
);
},
)
Put putAccountData
outside the build method but in the class like so:
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) {}
}
Upvotes: 1