text stringlengths 1 372 |
|---|
use code generation for medium to large projects |
JSON serialization with code generation means having an external library |
generate the encoding boilerplate for you. after some initial setup, |
you run a file watcher that generates the code from your model classes. |
for example, json_serializable and built_value are these |
kinds of libraries. |
this approach scales well for a larger project. no hand-written |
boilerplate is needed, and typos when accessing JSON fields are caught at |
compile-time. the downside with code generation is that it requires some |
initial setup. also, the generated source files might produce visual clutter |
in your project navigator. |
you might want to use generated code for JSON serialization when you have a |
medium or a larger project. to see an example of code generation based JSON |
encoding, see serializing JSON using code generation libraries. |
<topic_end> |
<topic_start> |
is there a GSON/Jackson/Moshi equivalent in flutter? |
the simple answer is no. |
such a library would require using runtime reflection, which is disabled in |
flutter. runtime reflection interferes with tree shaking, which dart has |
supported for quite a long time. with tree shaking, you can “shake off” unused |
code from your release builds. this optimizes the app’s size significantly. |
since reflection makes all code implicitly used by default, it makes tree |
shaking difficult. the tools cannot know what parts are unused at runtime, so |
the redundant code is hard to strip away. app sizes cannot be easily optimized |
when using reflection. |
although you cannot use runtime reflection with flutter, |
some libraries give you similarly easy-to-use APIs but are |
based on code generation instead. this |
approach is covered in more detail in the |
code generation libraries section. |
<topic_end> |
<topic_start> |
serializing JSON manually using dart:convert |
basic JSON serialization in flutter is very simple. flutter has a built-in |
dart:convert library that includes a straightforward JSON encoder and |
decoder. |
the following sample JSON implements a simple user model. |
<code_start> |
{ |
"name": "john smith", |
"email": "john@example.com" |
} |
<code_end> |
with dart:convert, |
you can serialize this JSON model in two ways. |
<topic_end> |
<topic_start> |
serializing JSON inline |
by looking at the dart:convert documentation, |
you’ll see that you can decode the JSON by calling the |
jsonDecode() function, with the JSON string as the method argument. |
<code_start> |
final user = jsonDecode(jsonString) as Map<String, dynamic>; |
print('Howdy, ${user['name']}!'); |
print('We sent the verification link to ${user['email']}.'); |
<code_end> |
unfortunately, jsonDecode() returns a dynamic, meaning |
that you do not know the types of the values until runtime. with this approach, |
you lose most of the statically typed language features: type safety, |
autocompletion and most importantly, compile-time exceptions. your code will |
become instantly more error-prone. |
for example, whenever you access the name or email fields, you could quickly |
introduce a typo. a typo that the compiler doesn’t know about since the |
JSON lives in a map structure. |
<topic_end> |
<topic_start> |
serializing JSON inside model classes |
combat the previously mentioned problems by introducing a plain model |
class, called user in this example. inside the user class, you’ll find: |
with this approach, the calling code can have type safety, |
autocompletion for the name and email fields, and compile-time exceptions. |
if you make typos or treat the fields as ints instead of strings, |
the app won’t compile, instead of crashing at runtime. |
user.dart |
<code_start> |
class user { |
final string name; |
final string email; |
user(this.name, this.email); |
User.fromJson(Map<String, dynamic> json) |
: name = json['name'] as string, |
email = json['email'] as string; |
Map<String, dynamic> toJson() => { |
'name': name, |
'email': email, |
}; |
} |
<code_end> |
the responsibility of the decoding logic is now moved inside the model |
itself. with this new approach, you can decode a user easily. |
<code_start> |
final userMap = jsonDecode(jsonString) as Map<String, dynamic>; |
final user = User.fromJson(userMap); |
print('Howdy, ${user.name}!'); |
print('We sent the verification link to ${user.email}.'); |
<code_end> |
to encode a user, pass the user object to the jsonEncode() function. |
you don’t need to call the toJson() method, since jsonEncode() |
already does it for you. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.