Search code examples
fluttersavelocal

how to locally save multiple address with shared preference flutter


i have a form to get user name , phonenumber, street , province and i want to save this in local with Shared Preferences like this to read i have card for each adress inside tha card it will show the details of the card so how can i do that?

{
"address1": {
    "name": "afthal",
    "phonenumber": "123456",
    "street": "197/F",
    "province": "Puttalam"
},
"address2": {
    "name": "afthal",
    "phonenumber": "123456",
    "street": "617/E Mannar",
    "province": "Colombo"
},
"address3": {
    "name": "afthal",
    "phonenumber": "123456",
    "street": "45 UC Road",
    "province": "Jaffna"
}

}


Solution

  • hope you are fine,

    The best solution is to use a local database, but if you insist to use shared preferences, you can do this approach:

    1. Encode the JSON and convert it to String.
    2. Save the string in the SharedPref.
    3. When you want to retrieve it -> get the String from SharedPref -> use json decode to decode it -> you will get. the Map (JSON) -> convert it to a list of addresses.