Flutter RefreshIndicator with a ListView populated from JSON

This Article is posted by seven.srikanth at 24-11-2018 17:08:41

In this article, I'm going to share an example of how to implement RefreshIndicator widget with a ListView which is populated from JSON. 

Here is the final main.dart code. All you need to do is to, copy paste this code into your newly created flutter project and run it.

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('https://jsonplaceholder.typicode.com/photos');

// Use the compute function to run parsePhotos in a separate isolate
return compute(parsePhotos, response.body);

// A function that will convert a response body into a List<Photo>
List<Photo> parsePhotos(String responseBody) {
final parsed = json.decode(responseBody).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;

Photo({this.albumId, this.id, this.title, this.url, 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(MyApp());

class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
home: MyHomePage(title: 'Flutter Demo Home Page'),

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

_MyHomePageState createState() => _MyHomePageState();

class _MyHomePageState extends State<MyHomePage> {
int _count = 0;

Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
body: Padding(
child: FutureBuilder<List<Photo>>(
future: fetchPhotos(http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);

return snapshot.hasData
? RefreshIndicator(
child: PhotosList(photos: snapshot.data, count: _count + 2),
onRefresh: _refreshhandle,
: Center(child: CircularProgressIndicator());
padding: EdgeInsets.fromLTRB(1.0, 10.0, 1.0, 10.0),

Future<Null> _refreshhandle() async {
setState(() {
return null;

class PhotosList extends StatelessWidget {
final List<Photo> photos;
final int count;
PhotosList({Key key, this.photos, this.count}) : super(key: key);
Widget build(BuildContext context) {
return ListView.builder(
itemCount: count,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
constraints: BoxConstraints.expand(
height: Theme.of(context).textTheme.display1.fontSize * 1.1 +
color: Colors.white10,
alignment: Alignment.center,
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
leading: Image.network(
photos[count - index - 1].thumbnailUrl,
fit: BoxFit.fitWidth,
title: Text(photos[count - index - 1].title),
subtitle: Text(photos[count - index - 1].title),
// make buttons use the appropriate styles for cards
child: ButtonBar(
children: <Widget>[
child: const Text('Open'),
onPressed: () {/* ... */},

Hope this example is helpful. 


Tags: Flutter RefreshIndicator with a ListView populated from JSON

Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.

Recent Articles

How to deploy flutter web site on GitHub using peanut

Posted by nikhilchaudhary12155 on 13 Oct 2019 Article

AnimatedPositioned Widget Example in Flutter

Posted by seven.srikanth on 10 Oct 2019 Article

Flutter Open-Source and directory lists

Posted by nishantkotatech on 09 Oct 2019 Article

Flutter Travelling Couple Leading Page - Flutter Web Series 3

Posted by nikhilchaudhary12155 on 05 Oct 2019 Article

© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com