0% found this document useful (0 votes)
3 views14 pages

Shared Preference

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Shared Preference

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

FLUTTER

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}.

The way of creating


object from
sharedprefrence
Shared Preference cont:
- Because shred preference uses Future, we need to use then() method
- Then() method carry an object of shared preference and you can use it as you want.

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

- Now I don’t have brothers value in shared preference.


Shared Preference : base code :
- class MyCache{
static SharedPreferences? preferences;
static Future<void>init()async{
preferences =await SharedPreferences.getInstance();
}
static void putString ({required MyCacheKeys key ,required String value})async{
await preferences?.setString(key.name, value);
}
// MyCacheKeys.email , [email protected]
// MyCacheKeys.password , 1314256
static String? getString ({required MyCacheKeys key }){
return preferences?.getString(key.name) ;
}
Shared Preference : base code :
-
void putInt({required MyCacheKeys key ,required int value}){
preferences?.setInt(key.name, value);
}
int getInt({required key }){
return preferences?.getInt(key) ?? 0;
}
void putBool({required MyCacheKeys key ,required bool value}){
preferences?.setBool(key.name, value);
}
bool getBool({required MyCacheKeys key }){
return preferences?.getBool(key.name) ?? false;
}
void putDouble({required MyCacheKeys key ,required double value}){
preferences?.setDouble(key.name, value);
}
Shared Preference : base code :
-
double getDouble({required MyCacheKeys key }){
return preferences?.getDouble(key.name) ?? 0.0;
}
void removeFromShared({required key})
{
preferences?.remove(key);
}
void clearShared(){
preferences?.clear();
}
Shared Preference:

- https://github.com/AMITFlutter10/sharedpreferences
THANK YOU!

You might also like