Created
July 5, 2018 04:24
-
-
Save diegoveloper/4dbb6b24034b887d81d8392d5c9355f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_samples/fetch_data/photo.dart'; | |
import 'package:http/http.dart' as http; | |
class MainFetchData extends StatefulWidget { | |
@override | |
_MainFetchDataState createState() => _MainFetchDataState(); | |
} | |
class _MainFetchDataState extends State<MainFetchData> { | |
List<Photo> list = List(); | |
var isLoading = false; | |
_fetchData() async { | |
setState(() { | |
isLoading = true; | |
}); | |
final response = | |
await http.get("https://jsonplaceholder.typicode.com/photos"); | |
if (response.statusCode == 200) { | |
list = (json.decode(response.body) as List) | |
.map((data) => new Photo.fromJson(data)) | |
.toList(); | |
setState(() { | |
isLoading = false; | |
}); | |
} else { | |
throw Exception('Failed to load photos'); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Fetch Data JSON"), | |
), | |
bottomNavigationBar: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: RaisedButton( | |
child: new Text("Fetch Data"), | |
onPressed: _fetchData, | |
), | |
), | |
body: isLoading | |
? Center( | |
child: CircularProgressIndicator(), | |
) | |
: ListView.builder( | |
itemCount: list.length, | |
itemBuilder: (BuildContext context, int index) { | |
return ListTile( | |
contentPadding: EdgeInsets.all(10.0), | |
title: new Text(list[index].title), | |
trailing: new Image.network( | |
list[index].thumbnailUrl, | |
fit: BoxFit.cover, | |
height: 40.0, | |
width: 40.0, | |
), | |
); | |
})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment