6.
DEVELOP AN APPLICATION TO CONNECT TO A WEB SERVICE
AND TO RETRIEVE DATA WITH HTTP
PROGRAM :
import 'package:flutter/[Link]';
import 'package:http/[Link]' as http;
import 'dart:convert';
void main() => runApp(MaterialApp(home: PostList()));
class PostList extends StatefulWidget {
@override
_PostListState createState() => _PostListState();
}
class _PostListState extends State<PostList> {
List posts = [];
@override
void initState() {
[Link]();
fetchPosts();
}
fetchPosts() async {
final response = await [Link]([Link]('[Link]
if ([Link] == 200) {
setState(() => posts = [Link]([Link]));
} else {
throw Exception("Failed to load posts");
}
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text("HTTP Data Fetch")),
body: [Link](
itemCount: [Link],
itemBuilder: (context, index) {
final post = posts[index];
return ListTile(
title: Text(post['title'], style: TextStyle(fontWeight: [Link])),
subtitle: Text(post['body']),
);
},
),
);
}
OUTPUT :