API Calling with HTTP Library (Sixth 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: "Fourth API",
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(
backgroundColor: Colors.grey,
centerTitle: true,
),
),
home: const FourthApi(),
);
}
}

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

@override
State<FourthApi> createState() {
return FourthApiState();
}
}

class FourthApiState extends State<FourthApi> {
Future<List<FourthModel>> fetchFourthModel() async {
final response = await http.get(
Uri.parse("https://jsonplaceholder.typicode.com/comments"),
);

if (response.statusCode == 200) {
Iterable jsonResponse = jsonDecode(response.body);
List<FourthModel> dataInputs = jsonResponse.map(
(model) {
return FourthModel.fromJson(model);
},
).toList();
return dataInputs;
} else {
throw Exception("Failed to load FourthModel");
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Fourth API Calling",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: Center(
child: FutureBuilder<List<FourthModel>>(
future: fetchFourthModel(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<FourthModel>? 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: 20.0),
),
),
);
},
);
} else if (snapshot.hasError) {
return Text("Error : ${snapshot.error}");
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
),
);
}
}

/// Model class
class FourthModel {
final int postId;
final int id;
final String name;
final String email;
final String body;

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

factory FourthModel.fromJson(Map<String, dynamic> json) {
return FourthModel(
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