text stringlengths 1 372 |
|---|
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), |
); |
} |
} |
class MyHomePage extends StatelessWidget { |
const MyHomePage({super.key, required this.title}); |
final string title; |
@override |
widget build(BuildContext context) { |
return scaffold( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.