Last active
April 13, 2020 13:17
-
-
Save long25vn/2cd38a35cb84174b26c19ff249e8a404 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Http Request Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
List<Post> _posts = []; | |
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = | |
new GlobalKey<RefreshIndicatorState>(); | |
bool _isLoading = false; | |
@override | |
initState() { | |
super.initState(); | |
fetchPosts(); | |
} | |
Future<dynamic> fetchPosts() { | |
_isLoading = true; | |
return http | |
.get('https://jsonplaceholder.typicode.com/posts') | |
.then((http.Response response) { | |
final List<Post> fetchedPosts = []; | |
final List<dynamic> postsData = json.decode(response.body); | |
if (postsData == null) { | |
setState(() { | |
_isLoading = false; | |
}); | |
} | |
for (var i = 0; i < postsData.length; i++) { | |
final Post post = Post( | |
userId: postsData[i]['userId'], | |
id: postsData[i]['id'], | |
title: postsData[i]['title'], | |
body: postsData[i]['body']); | |
fetchedPosts.add(post); | |
} | |
setState(() { | |
_posts = fetchedPosts; | |
_isLoading = false; | |
}); | |
}).catchError((Object error) { | |
setState(() { | |
_isLoading = false; | |
}); | |
}); | |
} | |
Future<dynamic> _onRefresh() { | |
return fetchPosts(); | |
} | |
Widget _buildPostList() { | |
return RefreshIndicator( | |
onRefresh: _onRefresh, | |
key: _refreshIndicatorKey, | |
child: ListView.builder( | |
itemBuilder: (BuildContext context, int index) { | |
return Column( | |
children: <Widget>[ | |
Padding( | |
child: new ListTile( | |
title: Text(_posts[index].title), | |
subtitle: Text(_posts[index].body), | |
), | |
padding: EdgeInsets.all(10.0), | |
), | |
Divider( | |
height: 5.0, | |
) | |
], | |
); | |
}, | |
itemCount: _posts.length, | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Posts'), | |
), | |
body: _isLoading | |
? Center( | |
child: CircularProgressIndicator(), | |
) | |
: _buildPostList(), | |
); | |
} | |
} | |
class Post { | |
final int userId; | |
final int id; | |
final String title; | |
final String body; | |
Post({ | |
@required this.userId, | |
@required this.id, | |
@required this.title, | |
@required this.body}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment