import 'dart:convert' ;
import 'package:flutter/material.dart' ;
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
//Creating a class user to store the data;
class User {
final int id;
final int userId;
final String title;
final String body;
User({
this .id,
this .userId,
this .title,
this .body,
});
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
//Applying get request.
Future> getRequest() async {
//replace your restFull API here.
String url = "https://jsonplaceholder.typicode.com/posts" ;
final response = await http.get(Uri.parse(url));
var responseData = json.decode(response.body);
//Creating a list to store input data;
List users = [];
for (var singleUser in responseData) {
User user = User(
id: singleUser[ "id" ],
userId: singleUser[ "userId" ],
title: singleUser[ "title" ],
body: singleUser[ "body" ]);
//Adding user to the list.
users.add(user);
}
return users;
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text( "Http Get Request." ),
leading: Icon(
Icons.get_app,
),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: FutureBuilder(
future: getRequest(),
builder: (BuildContext ctx, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (ctx, index) => ListTile(
title: Text(snapshot.data[index].title),
subtitle: Text(snapshot.data[index].body),
contentPadding: EdgeInsets.only(bottom: 20.0),
),
);
}
},
),
),
),
);
}
}
|
0 comments