Loading topic
dart:convert. Before you generate any code, you need a clear picture of what that library returns, which types you get back, and where the type system stops helping you. This chapter builds that model and covers the errors you will hit first.5 min readjsonDecode and know that decoded JSON arrives as Map<String, dynamic> and List<dynamic>. The goal is to see where hand-written fromJson and toJson code breaks down, so the generated cod5 min readpubspec.yaml, know what a Dart part file is at a basic level, and have worked through the previous chapters on dart:convert and manual serialization. You will add the three packages that make up the code-generation toolchain, check that they resolve correctl6 min readjson_annotation, json_serializable, and build_runner installed, as covered in the previous chapter. Here you write the model class that the generator reads. The next chap5 min readbuild_runner, what the generated file contains, and how to fix the errors that usually appear. It assumes you finished the previous chapter, so json_annotation, json_serializable, and build_runner are in pubspec.yaml, and lib/user.dart has th6 min read@JsonSerializable() assumes each Dart field maps to a JSON key of the same name and type. Real APIs often break that assumption: keys use a different style, fields are missing, or values arrive in an odd shape. @JsonKey lets you change how a single field is read and writt5 min read@JsonSerializable that apply to every field in a class: how Dart field names become JSON keys, and what happens to null values in each direction. It assumes you can annotate a model and run build_runner, and that you know @JsonKey from the previous chapter5 min read@JsonSerializable, run build_runner, and used @JsonKey and class-level options such as fieldRename. It covers what the generator does when a field is another model, a list of models, or a map of models. It also covers the one option that m5 min read, jsonserializable, and buildrunner set up, and you have already generated a .g.dart` file for an annotated model. If you have not,6 min read@JsonSerializable(), run build_runner, and read the generated .g.dart file. It also assumes you know Dart generics and const constructors.5 min read@JsonSerializable model, run build_runner, and call jsonDecode. It covers what fails when a server sends bad data, which exceptions you will see, and how checked: true turns vague errors into useful ones.4 min readfromJson and toJson and checks that nothing changed on the way. This chapter shows what to assert, how to avoid false passes, and how to cover the edge cases generated code cannot decide for you.5 min read{"data": ..., "message": ...} or a paginated list with a cursor. Writing one wrapper class per endpoint duplicates code. This chapter shows how to write one generic class, ApiResponse<T>, and have json_serializable generate its J4 min read@JsonSerializable, run build_runner, and call a REST endpoint with the http package. It also assumes you know what async and await do in Dart. You will learn why parsing a big response can make a Flutter app drop frames, ho6 min readjson_serializable with the other ways to turn JSON into Dart objects. It assumes you can already annotate a model, run build_runner, and read a generated .g.dart file, as covered in earlier chapters. It also assumes you know Dart 3 classes, records, and pattern matching a6 min read