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

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

@override
State<SeventhApi> createState() {
return SeventhApiState();
}
}

class SeventhApiState extends State<SeventhApi> {
@override
void initState() {
super.initState();
fetchSeventhModel();
}

Future<List<SeventhModel>> fetchSeventhModel() async {
final response = await http.get(
Uri.parse("https://jsonplaceholder.typicode.com/photos"),
);

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

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Seventh API Calling with HTTP Library",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: Center(
child: FutureBuilder<List<SeventhModel>>(
future: fetchSeventhModel(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<SeventhModel>? 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(
data[index].title,
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text(
"Url : ${data[index].url}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text("Thumbnailurl : ${data[index].thumbnailUrl}"),
],
),
trailing: Text(
data[index].albumId.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 SeventhModel {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;

SeventhModel({
required this.albumId,
required this.id,
required this.title,
required this.url,
required this.thumbnailUrl,
});

factory SeventhModel.fromJson(Map<String, dynamic> json) {
return SeventhModel(
albumId: json["albumId"],
id: json["id"],
title: json["title"],
url: json["url"],
thumbnailUrl: json["thumbnailUrl"],
);
}
}


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