Saving Data in Flutter

Jan 31 2024 · Dart 3, Flutter 3.10, Visual Studio Code

Part 3: Reading & Writing Files

16. Using JSON (Part 1)

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: 15. Polishing the App Next episode: 17. Using JSON (Part 2)

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've reached locked video content where the transcript will be shown as obfuscated text.

There’s a small change we can implement in our app, that will have no impact on the user experience, but can have several benefits for us as developers: it’s leveraging Dart objects and JSON instead of storing simple data in SharedPreferences.
I’ve told you previously, that SharedPreferences only accepts simple data, like strings, numbers, and Booleans, and this is the way we are using SharedPreferences right now.

class AppSettings { 
  String listName = ''; 
  int calories = 0; 
  bool showFileSize = false; 
  bool showDate = false; 
} 
Map<String, dynamic> toJson() { 
    return <String, dynamic>{ 
      'listName': listName, 
      'calories': calories, 
      'showFileSize': showFileSize, 
      'showDate': showDate, 
    }; 
  } 
AppSettings.fromJson(Map<String, dynamic> json) 
  : listName = json['listName'] is String 
        ? json['listName'] as String 
        : 'My Recipes', 
    calories = json['calories'] is int ? json['calories'] as int : 2000, 
    showFileSize = 
        json['showFileSize'] is bool ? json['showFileSize'] as bool : true, 
    showDate = json['showDate'] is bool ? json['showDate'] as bool : true;