Your Second Flutter App

Nov 30 2021 · Dart 2.13, Flutter 2.2.3, Visual Studio Code

Part 4: Filter Results

28. Use Shared Preferences

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 27. Challenge: Add a Filter Next episode: 29. Filter Courses

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Another key part of providing a solid user experience in your app is saving data that your user has entered so that it survives between app restarts.

https://pub.dev/packages/shared_preferences
flutter pub add shared_preferences
void _loadValue() {

}
  _loadValue() async {
    final prefs = await SharedPreferences.getInstance();
  }
import 'package:shared_preferences/shared_preferences.dart';
void _loadValue() async {
  final prefs = await SharedPreferences.getInstance();
  setState(() {
    _filterValue = prefs.getInt(Constants.filterKey) ?? 0;
  });
}
class _FilterPageState extends State<FilterPage> {
  int _filterValue = Constants.allFilter;

  @override
  void initState() {
    super.initState();

    _loadValue();
  }
  void _handleRadioValueChange(int? value) async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      _filterValue = value ?? 0;
      prefs.setInt(Constants.filterKey, _filterValue);
    });
  }