Back

[SOLVED]How to update Account Preferences with string.

  • 0
  • Android
Drake
6 Jun, 2023, 15:45

can you do something like getPrefs<Map<String, Object>>()?

TL;DR
The user had a question about updating account preferences with a string. Another user provided a suggested code solution for updating preferences using a map and demonstrated how to add, remove, and update preferences. The user had some clarifying questions and errors with their code, but the solution was not provided in the support thread.
retr122
6 Jun, 2023, 16:05

no

Drake
6 Jun, 2023, 16:09

what's the error?

retr122
6 Jun, 2023, 16:16

this method returns io.appwrite.Preferences

Drake
6 Jun, 2023, 16:22

So? My suggestion would make it return Preferences<Map<String, Object>>

retr122
6 Jun, 2023, 16:26

I had one idea and check this

retr122
6 Jun, 2023, 16:26

This gives me a new error

retr122
6 Jun, 2023, 16:26

I rewrite code as

retr122
6 Jun, 2023, 16:27
TypeScript
                                    Map<String, Object> updatedAccountPrefsMap = (Map<String, Object>) updatedAccountPrefs.getData();
                                    account.updatePrefs(
                                            Objects.requireNonNull(updatedAccountPrefsMap.replace("main_info", newProfileBio)),
...```
retr122
6 Jun, 2023, 16:27

and catch io.appwrite.exceptions.AppwriteException: Invalid prefs: Value must be a valid object.

retr122
6 Jun, 2023, 16:33

newProfileBio is a String

retr122
6 Jun, 2023, 16:46

Can I use JSONObject?

Drake
6 Jun, 2023, 17:12

There's no need to use Objects.requireNonNull()

Drake
6 Jun, 2023, 17:13

what happened when you tried getPrefs<Map<String, Object>>()?

Drake
6 Jun, 2023, 17:19

last resort would be to create a new HashMap<String, Object>, add the entries from the existing prefs into it, and then pass it to updatePrefs()

retr122
6 Jun, 2023, 17:36

this is false in syntacs

Drake
6 Jun, 2023, 18:20

What do you mean?

Drake
6 Jun, 2023, 18:22

does getPrefs accept any arguments?

Binyamin
6 Jun, 2023, 23:16

Okay, I've found the time to test it out.

It should be something like this.

TypeScript
// First get the map.
Map<String, Object> map = (Map<String, Object>) pref.getData();

// You can add new pref
map.put("pref", "value");

// You can delete a pref
map.remove("pref");

// You can update pref.
map.replace("pref", "newValue");

// Then, update it
account.updatePrefs(map,  /*... */);

It's important you'll do all adding/removing/updating process on a second variable, as none of all those methods will return the object it self.

retr122
7 Jun, 2023, 03:59

Yes, this is correct answer! Thank you very much, it's working!

retr122
7 Jun, 2023, 04:00

But I have one question about other theme (coding isn't need)

retr122
7 Jun, 2023, 04:01

Where can I ask this? (not now)

Drake
7 Jun, 2023, 04:01

Create a new post in <#1072905050399191082>

retr122
7 Jun, 2023, 04:01

Ok

retr122
7 Jun, 2023, 06:43

[SOLVED]How to update Account Preferences with string.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more