data/models/enum_example library
Example: Real-world enum serialization with helper functions
This example demonstrates the helper function pattern for safe enum serialization that handles unknown values gracefully.
KEY INSIGHT: json_serializable IGNORES @Converter annotations on non-nullable enums. We must use @JsonKey(fromJson:, toJson:) to explicitly tell it to call our helper functions.
PATTERN:
- Define enum (no "unknown" values needed)
- Create top-level _deserialize and _serialize functions for each enum
- Use @JsonKey(fromJson: _deserializeFoo, toJson: _serializeFoo) on fields
Classes
- NotificationModel
- PostModel
- SocialMediaService
- Example service showing how these models handle enum updates gracefully
- UserProfileModel