text stringlengths 1 372 |
|---|
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, |
id: json['id'] as int, |
title: json['title'] as string, |
url: json['url'] as string, |
thumbnailUrl: json['thumbnailUrl'] as string, |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
convert the response into a list of photos |
now, use the following instructions to update the |
fetchPhotos() function so that it returns a |
Future<List<Photo>>: |
<code_start> |
// a function that converts a response body into a List<Photo>. |
List<Photo> parsePhotos(String responseBody) { |
final parsed = |
(jsondecode(responsebody) as List).cast<Map<String, dynamic>>(); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.