2

I have saved json data in SharedPreferences like this

storage.setString("user", userInfo);

now I want to edit value inside userInfo which consist of name & phone number , so what I can do to change the value like the name

2 Answers 2

2
// get your json
var source = prefs.getString('user');
var json = jsonDecode(source);

// make changes
json['userInfo'] = {
  'name': 'new name',
  'phoneNo': 'new number'
};

// put it back
prefs.setString('user', jsonEncode(json));
Sign up to request clarification or add additional context in comments.

3 Comments

Glad to hear that, I also added a way to show how you can edit your json file.
Yes, I appreciate your effort for sure <3 , so is it the best way to edit the user profile ?
I am not sure what the context is here, so can't recommend anything without seeing your app logic.
1

first of all import the convert library;

import 'dart:convert';

get your jsonString

String jsonString = await storage.getString("user");

and decode it to Dart;

var yourJson = json.decode(jsonString);

Edit your json and convert it to String again;

String editedJsonString = json.encode(yourJson); 

finally save it to shared with same key;

storage.setString("user", editedJsonString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.