text stringlengths 1 474 |
|---|
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>>(); |
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList(); |
} |
Future<List<Photo>> fetchPhotos(http.Client client) async { |
final response = await client |
.get(Uri.parse('https://jsonplaceholder.typicode.com/photos')); |
// Synchronously run parsePhotos in the main isolate. |
return parsePhotos(response.body); |
}<code_end> |
<topic_end> |
<topic_start> |
4. Move this work to a separate isolate |
If you run the fetchPhotos() function on a slower device, |
you might notice the app freezes for a brief moment as it parses and |
converts the JSON. This is jank, and you want to get rid of it.You can remove the jank by moving the parsing and conversion |
to a background isolate using the compute() |
function provided by Flutter. The compute() function runs expensive |
functions in a background isolate and returns the result. In this case, |
run the parsePhotos() function in the background. |
<code_start>Future<List<Photo>> fetchPhotos(http.Client client) async { |
final response = await client |
.get(Uri.parse('https://jsonplaceholder.typicode.com/photos')); |
// Use the compute function to run parsePhotos in a separate isolate. |
return compute(parsePhotos, response.body); |
}<code_end> |
<topic_end> |
<topic_start> |
Notes on working with isolates |
Isolates communicate by passing messages back and forth. These messages can |
be primitive values, such as null, num, bool, double, or String, or |
simple objects such as the List<Photo> in this example.You might experience errors if you try to pass more complex objects, |
such as a Future or http.Response between isolates.As an alternate solution, check out the worker_manager or |
workmanager packages for background processing.<topic_end> |
<topic_start> |
Complete example |
<code_start>import 'dart:async'; |
import 'dart:convert'; |
import 'package:flutter/foundation.dart'; |
import 'package:flutter/material.dart'; |
import 'package:http/http.dart' as http; |
Future<List<Photo>> fetchPhotos(http.Client client) async { |
final response = await client |
.get(Uri.parse('https://jsonplaceholder.typicode.com/photos')); |
// Use the compute function to run parsePhotos in a separate isolate. |
return compute(parsePhotos, response.body); |
} |
// 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>>(); |
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList(); |
} |
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, |
); |
} |
} |
void main() => runApp(const MyApp()); |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
Widget build(BuildContext context) { |
const appTitle = 'Isolate Demo'; |
return const MaterialApp( |
title: appTitle, |
home: MyHomePage(title: appTitle), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.