0
0
Fluttermobile~3 mins

Why SharedPreferences for key-value in Flutter? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your app could remember user choices instantly without complex code?

The Scenario

Imagine you want your app to remember a user's name or theme choice every time they open it. Without a simple way to save this small info, you'd have to ask the user again and again, which feels annoying and unprofessional.

The Problem

Trying to save data manually means writing complex code to store files or databases just for tiny bits of info. This is slow, easy to mess up, and wastes time that could be spent making your app better.

The Solution

SharedPreferences lets you save and get small pieces of data like strings or numbers easily. It works like a tiny notebook your app can quickly read from or write to, so user choices stay saved without hassle.

Before vs After
Before
File file = File('user.txt');
await file.writeAsString('username=John');
String name = file.readAsStringSync();
After
final prefs = await SharedPreferences.getInstance();
await prefs.setString('username', 'John');
String? name = prefs.getString('username');
What It Enables

It makes saving user settings and preferences simple, fast, and reliable, improving user experience instantly.

Real Life Example

Think about a weather app that remembers your city so you don't have to type it every time you open it. SharedPreferences makes that easy.

Key Takeaways

Manual saving is slow and error-prone for small data.

SharedPreferences offers a quick way to store key-value pairs.

This improves app usability by remembering user choices.