Shared Preference
Shared Preference
SHARED PREFERENCE
Shared Preference
• Shared Preference
Shared Preference:
- Did you ask yourself how for example Facebook doesn’t ask you to enter your email and password
for each time you open the app.
- Once you logged in first time, it saved email and password.
- So it may save your date ( email and password ) on your device and once you open the app, it will
get email and password and make login operation automatically, so you see the home page directly.
Shared Preference:
- One of the ways which used to save data on device to be ( offline data base ) is Shared Preference.
- How can you use it?
- Install shared_preference packge.
- Run this command in your project terminal : flutter pub add shared_preference.
- Import the package in your dart file.
- Start. HOW?
Shared Preference:
- If you have two screens if your app (login and home ), and you need to check if this user is logged in
or first time and he need to login.
- Convert MyAppWidget to Stateless Widget.
- Take an object from ShredPreference.
- On initState function check if this user has data stored about him or not.
- Depending on result, you have to choose which screen he should see first.
- But how can I get, set delete or update values?
- After you took an object from sharedprefrence , you can do what you want by the
methods provided in this object.
- You know your data is stored as {key:value}.
Inside this
function you can
do what you
Object of
want on shared
shared
preference
preference
object : (value)
Shared Preference : set values:
- When setting values, you should set it as {key:value} . Ex: {“passwor”,”123456789”},
- So the first parameter of any set method is string (key).
Shared Preference : get values:
- If you want to know any value, you can access it by using the key of value
Key
Shared Preference : remove values:
- If you want to remove any value, you can remove it by using the key of value
- https://github.com/AMITFlutter10/sharedpreferences
THANK YOU!