Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (86 sloc) 2.11 KB
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(HttpApp());
}
class Post {
final int userId;
final int id;
final String title;
Post({
this.userId,
this.id,
this.title,
});
factory Post.fromJSON(Map<String, dynamic> json) {
return Post(
id: json['id'],
userId: json['userId'],
title: json['title'],
);
}
}
class HttpApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HttpExampleWidget(),
);
}
}
class HttpExampleWidget extends StatefulWidget {
@override
_HttpExampleWidgetState createState() => _HttpExampleWidgetState();
}
class _HttpExampleWidgetState extends State<HttpExampleWidget> {
List<Post> _posts = [];
void _fetchPosts() async {
final response =
await http.get('https://jsonplaceholder.typicode.com/posts');
final List<Post> parsedResponse = jsonDecode(response.body)
.map<Post>((json) => Post.fromJSON(json))
.toList();
setState(() {
_posts.clear();
_posts.addAll(parsedResponse);
});
}
void _createPost() async {
final response = await http.post(
'https://jsonplaceholder.typicode.com/posts',
body: jsonEncode(
{
'title': 'foo',
'body': 'bar',
'userId': 1,
},
),
headers: {'Content-Type': "application/json"},
);
final Post parsedResponse = Post.fromJSON(jsonDecode(response.body));
// Use the parsedResponse to diplay the newly created post.
}
@override
void initState() {
super.initState();
_fetchPosts();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView.builder(
itemCount: this._posts.length,
itemBuilder: (context, index) {
final post = this._posts[index];
return ListTile(
title: Text(post.title),
subtitle: Text('Id: ${post.id} UserId: ${post.userId}'),
);
},
),
),
);
}
}
You can’t perform that action at this time.