text stringlengths 1 474 |
|---|
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); |
String name; |
Address address; |
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json); |
Map<String, dynamic> toJson() => _$UserToJson(this); |
}<code_end> |
For more information, see explicitToJson in the |
JsonSerializable class for the json_annotation package.<topic_end> |
<topic_start> |
Further references |
For more information, see the following resources: |
<topic_end> |
<topic_start>Parse JSON in the background |
By default, Dart apps do all of their work on a single thread. |
In many cases, this model simplifies coding and is fast enough |
that it does not result in poor app performance or stuttering animations, |
often called “jank.”However, you might need to perform an expensive computation, |
such as parsing a very large JSON document. |
If this work takes more than 16 milliseconds, |
your users experience jank.To avoid jank, you need to perform expensive computations |
like this in the background. |
On Android, this means scheduling work on a different thread. |
In Flutter, you can use a separate Isolate. |
This recipe uses the following steps:<topic_end> |
<topic_start> |
1. Add the http package |
First, add the http package to your project. |
The http package makes it easier to perform network |
requests, such as fetching data from a JSON endpoint.To add the http package as a dependency, |
run flutter pub add:<topic_end> |
<topic_start> |
2. Make a network request |
This example covers how to fetch a large JSON document |
that contains a list of 5000 photo objects from the |
JSONPlaceholder REST API, |
using the http.get() method. |
<code_start>Future<http.Response> fetchPhotos(http.Client client) async { |
return client.get(Uri.parse('https://jsonplaceholder.typicode.com/photos')); |
}<code_end> |
info Note |
You’re providing an http.Client to the function in this example. |
This makes the function easier to test and use in different environments.<topic_end> |
<topic_start> |
3. Parse and convert the JSON into a list of photos |
Next, following the guidance from the |
Fetch data from the internet recipe, |
convert the http.Response into a list of Dart objects. |
This makes the data easier to work with.<topic_end> |
<topic_start> |
Create a Photo class |
First, create a Photo class that contains data about a photo. |
Include a fromJson() factory method to make it easy to create a |
Photo starting with a JSON object. |
<code_start>class Photo { |
final int albumId; |
final int id; |
final String title; |
final String url; |
final String thumbnailUrl; |
const Photo({ |
required this.albumId, |
required this.id, |
required this.title, |
required this.url, |
required this.thumbnailUrl, |
}); |
factory Photo.fromJson(Map<String, dynamic> json) { |
return Photo( |
albumId: json['albumId'] as int, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.