Component - SharedPreferences for key-value
This UI component shows how to save and load simple key-value data using SharedPreferences in Flutter. It has a text input to enter a value, a button to save it, and a text display to show the saved value.
This UI component shows how to save and load simple key-value data using SharedPreferences in Flutter. It has a text input to enter a value, a button to save it, and a text display to show the saved value.
Scaffold
├─ AppBar
│ └─ Text
└─ Padding
└─ Column
├─ TextField
├─ ElevatedButton
└─ TextSharedPreferences in a Flutter app?true with key isLoggedIn using SharedPreferences in Flutter?setBool.final prefs = await SharedPreferences.getInstance();
await prefs.setInt('counter', 5);
int count = prefs.getInt('counter') ?? 0;
print(count);setInt.getInt. Since 'counter' exists, it returns 5, not the default 0.final prefs = SharedPreferences.getInstance();
prefs.setString('username', 'Alice');getInstance() returns a Future, so it must be awaited or handled asynchronously.setString on a Future object (not the SharedPreferences instance) causes error.Pass the test and this pattern moves unknown