How to use Flutter Local Storage Data

When storing Tokens or essential info which will be re-used when your flutter application opens, its best to store those values on localstorage. How to use flutter shared preference to store values on flutter local storage.


The fist thing to add flutter shared preference on your dependencies


shared_preferences: ^2.0.5


Then use the shared_preference from your code

SharedPreferences prefs = await SharedPreferences.getInstance();

How to use flutter shared_preferences to store data ​from flutter localstorage

await prefs.setString("your key", "your value string");


How to use flutter shared_preferences to remove data ​ from flutter localstorage

prefs.remove("apiToken");


How to use flutter shared_preferences to get data from flutter localstorage


String _token = prefs.getString("apiToken") ?? null;