flutter-implement-json-serialization
Installation
SKILL.md
Contents
- Core Guidelines
- Dart 3 Pattern Matching in fromJson
- Background Parsing
- Manual vs Code-Gen Decision
- Workflow: Implementing a Serializable Model
- Workflow: Fetching and Parsing JSON
- Examples
Core Guidelines
- Import
dart:convert: UsejsonEncode()andjsonDecode()for manual serialization. - Type Safety: Always cast
jsonDecode()result toMap<String, dynamic>(objects) orList<dynamic>(arrays). Never work with rawdynamic. - Encapsulation: Define
fromJsonfactory constructor andtoJsonmethod within the model class. - Background Parsing: Offload to a separate isolate via
compute()if parsing takes > 16ms (large JSON payloads). - Error Handling: Throw
FormatExceptionon invalid JSON. Never returnnullfromfromJson.