API Calling with HTTP Library (Fifth API)

 import "dart:convert";

import "package:flutter/material.dart";
import "package:http/http.dart" as http;

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Fifth API Integration",
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(
backgroundColor: Colors.grey,
centerTitle: true,
),
),
home: const FifthApi(),
);
}
}

class FifthApi extends StatefulWidget {
const FifthApi({super.key});

@override
State<FifthApi> createState() {
return FifthApiState();
}
}

class FifthApiState extends State<FifthApi> {

Future<List<FifthModel>> fetchFifthModel() async {
final response = await http.get(
Uri.parse("https://jsonplaceholder.typicode.com/posts/1/comments"),
);

if (response.statusCode == 200) {
Iterable jsonResponse = jsonDecode(response.body);

List<FifthModel> dataInputs = jsonResponse.map(
(model) {
return FifthModel.fromJson(model);
},
).toList();
return dataInputs;
} else {
throw Exception("Failed to load Exception");
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Fifth API Integration",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: Center(
child: FutureBuilder<List<FifthModel>>(
future: fetchFifthModel(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<FifthModel>? data = snapshot.data;
return ListView.builder(
itemCount: data!.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(
data[index].id.toString(),
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Name : ${data[index].name}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text(
"Email : ${data[index].email}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text("Body : ${data[index].body}"),
],
),
trailing: Text(
data[index].postId.toString(),
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 15.0),
),
),
);
},
);
} else if (snapshot.hasError) {
return Text("Error : ${snapshot.error}");
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
),
);
}
}

class FifthModel {
final int postId;
final int id;
final String name;
final String email;
final String body;

FifthModel(
{required this.postId,
required this.id,
required this.name,
required this.email,
required this.body});

factory FifthModel.fromJson(Map<String, dynamic> json) {
return FifthModel(
postId: json["postId"] as int,
id: json["id"] as int,
name: json["name"] as String,
email: json["email"] as String,
body: json["body"] as String,
);
}
}



Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter