text
stringlengths
1
372
you’ll get errors similar to what is shown in the image below.
these errors are entirely normal and are simply because the generated code for
the model class does not exist yet. to resolve this, run the code
generator that generates the serialization boilerplate.
there are two ways of running the code generator.
<topic_end>
<topic_start>
one-time code generation
by running dart run build_runner build --delete-conflicting-outputs in the project root,
you generate JSON serialization code for your models whenever they are needed.
this triggers a one-time build that goes through the source files, picks the
relevant ones, and generates the necessary serialization code for them.
while this is convenient, it would be nice if you did not have to run the
build manually every time you make changes in your model classes.
<topic_end>
<topic_start>
generating code continuously
a watcher makes our source code generation process more convenient. it
watches changes in our project files and automatically builds the necessary
files when needed. start the watcher by running
dart run build_runner watch --delete-conflicting-outputs in the project root.
it is safe to start the watcher once and leave it running in the background.
<topic_end>
<topic_start>
consuming json_serializable models
to decode a JSON string the json_serializable way,
you do not have actually to make any changes to our previous code.
<code_start>
final userMap = jsonDecode(jsonString) as Map<String, dynamic>;
final user = User.fromJson(userMap);
<code_end>
the same goes for encoding. the calling API is the same as before.
<code_start>
string json = jsonEncode(user);
<code_end>
with json_serializable,
you can forget any manual JSON serialization in the user class.
the source code generator creates a file called user.g.dart,
that has all the necessary serialization logic.
you no longer have to write automated tests to ensure
that the serialization works—it’s now
the library’s responsibility to make sure the serialization works
appropriately.
<topic_end>
<topic_start>
generating code for nested classes
you might have code that has nested classes within a class.
if that is the case, and you have tried to pass the class in JSON format
as an argument to a service (such as firebase, for example),
you might have experienced an invalid argument error.
consider the following address class:
<code_start>
import 'package:json_annotation/json_annotation.dart';
part 'address.g.dart';
@jsonserializable()
class address {
string street;
string city;
address(this.street, this.city);
factory Address.fromJson(Map<String, dynamic> json) =>
_$AddressFromJson(json);
Map<String, dynamic> toJson() => _$AddressToJson(this);
}
<code_end>
the address class is nested inside the user class:
<code_start>
import 'package:json_annotation/json_annotation.dart';
import 'address.dart';
part 'user.g.dart';
@jsonserializable()
class user {
user(this.name, this.address);
string name;
address address;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
<code_end>
running
dart run build_runner build --delete-conflicting-outputs
in the terminal creates
the *.g.dart file, but the private _$UserToJson() function
looks something like the following:
all looks fine now, but if you do a print() on the user object:
<code_start>
address address = Address('My st.', 'new york');
user user = User('John', address);
print(user.toJson());
<code_end>
the result is:
when what you probably want is output like the following:
to make this work, pass explicitToJson: true in the @jsonserializable()
annotation over the class declaration. the user class now looks as follows:
<code_start>
import 'package:json_annotation/json_annotation.dart';
import 'address.dart';
part 'user.g.dart';
@jsonserializable(explicittojson: true)
class user {
user(this.name, this.address);