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>> getPhotos(http.Client client) async {
final response =
return
compute(Photos_parser, response.body);
}
List<Photo> Photos_parser(String responseBody) {
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
return
parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}
class
Photo_list{
final
int
Photo_albumId;
final
int
Photo_id;
final String Photo_title;
final String Photo_url;
final String Photo_thumbnailUrl;
Photo_list({
this
.Photo_albumId,
this
.Photo_id,
this
.Photo_title,
this
.Photo_url,
this
.Photo_thumbnailUrl});
factory Photo_list.fromJson(Map<String, dynamic> json) {
return
Photo_list(
Photo_albumId: json[
'albumId'
] as
int
,
Photo_id: json[
'id'
] as
int
,
Photo_title: json[
'title'
] as String,
Photo_url: json[
'url'
] as String,
Photo_thumbnailUrl: json[
'thumbnailUrl'
] as String,
);
}
}
void
main() => runApp(MyApp());
class
MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appTitle =
'Isolate Demo'
;
return
MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class
HomePage extends StatelessWidget {
final String title;
HomePage({Key key,
this
.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(
title: Text(title),
),
body: FutureBuilder<List<Photo>>(
future: fetchPhotos(http.Client()),
builder: (context, snapshot) {
if
(snapshot.hasError) print(snapshot.error);
return
snapshot.hasData
? PhotosList(photos: snapshot.data)
: Center(child: CircularProgressIndicator());
},
),
);
}
}
class
PhotosList extends StatelessWidget {
final List<Photo> photos;
PhotosList({Key key,
this
.photos}) : super(key: key);
@override
Widget build(BuildContext context) {
return
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: photos.length,
itemBuilder: (context, index) {
return
Image.network(photos[index].Photo_thumbnailUrl);
},
);
}
}