tezvyn:

Persist a custom Dart object using shared_preferences

AI-drafted, machine-checkedSource: dart.devintermediate

Tests JSON serialization bridging custom objects to primitive-only key-value storage. Strong answer: add toJson/fromJson on User, jsonEncode into SharedPreferences as String, jsonDecode on read. Red flag: proposing direct storage or toString hacks.

WHAT THIS TESTS: This question evaluates whether you understand that shared_preferences is a primitive-only key-value store and that JSON serialization is the standard bridge pattern for custom objects. It also checks your familiarity with dart:convert, manual serialization methods, and type-safe reconstruction in Dart.

A GOOD ANSWER COVERS: A good answer hits four things in order. First, acknowledge the constraint: SharedPreferences only supports String, int, double, bool, and List<String>, so a User object cannot be stored directly. Second, implement serialization methods on the User class: a toJson method that returns Map<String, dynamic> and a fromJson factory constructor that accepts the same map type. Third, use dart:convert to bridge the gap: call jsonEncode on the map to produce a String before calling prefs.setString, and call jsonDecode after prefs.getString to recover the map. Fourth, reconstruct the User by passing the decoded map into User.fromJson. Strong candidates also mention async/await because SharedPreferences operations are asynchronous.

COMMON WRONG ANSWERS: The biggest red flag is suggesting you can store the User instance directly or call prefs.setObject. Another red flag is using toString on the User and then attempting manual string parsing or regex extraction on retrieval instead of structured JSON. A third red flag is forgetting that jsonDecode returns dynamic, which can lead to runtime crashes if you do not cast or validate the result before passing it to fromJson.

LIKELY FOLLOW-UPS: The interviewer may ask how you would handle nested objects or lists of Users, which just means the same pattern applied recursively. They may also ask about code generation with json_serializable or freezed to avoid boilerplate. Another common follow-up is error handling: what happens if the key is missing or the JSON is malformed? A senior answer discusses try-catch blocks, default values, or migrating data shapes when the model changes.

ONE CONCRETE EXAMPLE: Suppose you have a User with name Alice and age 30. You define a toJson method that returns a map with string keys name and age, plus a fromJson factory that reads those keys and casts them to String and int. To save, you await prefs.setString using a storage key and jsonEncode on user.toJson. To load, you read the string with prefs.getString, return a default User if it is null, otherwise return User.fromJson after calling jsonDecode and casting the result to the expected map type.

Read the original → dart.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.