Skip to content

Instantly share code, notes, and snippets.

@simonpham
Last active March 28, 2024 11:06
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save simonpham/4aaab5a8ddfcae06fdb0057aeb6230b8 to your computer and use it in GitHub Desktop.
Save simonpham/4aaab5a8ddfcae06fdb0057aeb6230b8 to your computer and use it in GitHub Desktop.
/// Use SharedPreferences in Flutter effortlessly
/// https://medium.com/@simonpham/use-sharedpreferences-in-flutter-effortlessly-835bba8f7418
// utils/shared_prefs.dart
class SharedPrefs {
late final SharedPreferences _sharedPrefs;
static final SharedPrefs _instance = SharedPrefs._internal();
factory SharedPrefs() => _instance;
SharedPrefs._internal();
Future<void> init() async {
_sharedPrefs = await SharedPreferences.getInstance();
}
String get username => _sharedPrefs.getString(keyUsername) ?? "";
set username(String value) {
_sharedPrefs.setString(keyUsername, value);
}
}
// constants/strings.dart
const String keyUsername = "key_username";
// main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await SharedPrefs().init();
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Text("Hi ${SharedPrefs().username}"),
),
);
}
}
@ripesol
Copy link

ripesol commented Nov 7, 2020

how to assign a value to sharedPrefs.username ?

@simonpham
Copy link
Author

how to assign a value to sharedPrefs.username ?

@ripesol just call sharedPrefs.username = "someName";

@vikasofvikas
Copy link

Thank you so much! , It's so easy to use shared preferences with your code.....They should have added your code below their official documentation.....It's so easy to use...
Thank you seariously!!!

@Babsky
Copy link

Babsky commented Apr 24, 2021

Awesome! So easy!

@devcastoro
Copy link

Hi, I am new to Flutter and Dart, so maybe I am just ignoring something very trivial.

I am using your exact code, but when I run the app, I receive this error referring to row 6:

Error: Field '_sharedPrefs' should be initialized because its type 'SharedPreferences' doesn't allow
null.

@simonpham
Copy link
Author

Hi, I am new to Flutter and Dart, so maybe I am just ignoring something very trivial.

I am using your exact code, but when I run the app, I receive this error referring to row 6:

Error: Field '_sharedPrefs' should be initialized because its type 'SharedPreferences' doesn't allow
null.

I have updated the code to be compatible with null-safety. Thank you for reporting!

@elinonga
Copy link

I have updated the code to be compatible with null-safety. Thank you for reporting!

So where is the update?

@simonpham
Copy link
Author

So where is the update?

@elinonga it's updated here.

@maverick42
Copy link

Awesome. Thx !

@simonpham
Copy link
Author

Hi folks 👋
I have release a new package easy_hive, which is a better and simpler approach for storing key-value data instead of using shared_preferences. Please have a look.
Thank you so much!

🦊

@Lammpo
Copy link

Lammpo commented Apr 8, 2023

Hi! I'm using this code but I get this error:
Undefined name '_internal'. Try correcting the name to one that is defined, or defining the name.

Thank you for your post in medium!

@simonpham
Copy link
Author

Hi! I'm using this code but I get this error: Undefined name '_internal'. Try correcting the name to one that is defined, or defining the name.

Make sure you didn't miss this line:

  SharedPrefs._internal();

Anyway, please check my above comment.

@Lammpo
Copy link

Lammpo commented Apr 11, 2023

I'm sry I meant '_instance'. Anyway I'm checking your awesome package

@simonpham
Copy link
Author

I'm sry I meant '_instance'. Anyway I'm checking your awesome package

I have updated the code. Apologize for my mistake.

  static final SharedPrefs _instance = SharedPrefs._internal();

@Dhruvmtzinfotech
Copy link

how many years experience in flutter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment