API Calling with Dio Library (Seventh API)

 import "package:flutter/material.dart";

import "package:dio/dio.dart";

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 Dio",
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(
backgroundColor: Colors.grey,
centerTitle: true,
),
),
home: const SeventhDio(),
);
}
}

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

@override
State<SeventhDio> createState() {
return SeventhDioState();
}
}

class SeventhDioState extends State<SeventhDio> {
var jsonlist = [];

@override
void initState() {
super.initState();
getData();
}

void getData() async {
try {
final response =
await Dio().get("https://jsonplaceholder.typicode.com/photos");

if (response.statusCode == 200) {
setState(
() {
jsonlist = response.data as List;
},
);
print(response);
} else {
print(response.statusCode);
}
} catch (e) {
print(e);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Seventh API Calling with Dio",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: Center(
child: ListView.builder(
itemCount: jsonlist == null ? 0 : jsonlist.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(
jsonlist[index]["id"].toString(),
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
jsonlist[index]["title"],
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text(
"Url : ${jsonlist[index]["url"]}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text("Thumbnailurl : ${jsonlist[index]["thumbnailUrl"]}"),
],
),
trailing: Text(
jsonlist[index]["albumId"].toString(),
style: const TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
),
),
);
},
),
),
);
}
}


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