API Calling with Dio Library (Sixth API)

 import "package:flutter/material.dart";

import "package:dio/dio.dart";

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

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

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

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

@override
State<SixthDio> createState() {
return SixthDioState();
}
}

class SixthDioState extends State<SixthDio> {
var jsonlist = [];

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

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

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

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Sixth API Calling with Dio",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: 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: Text(
jsonlist[index]["title"],
style: const TextStyle(fontWeight: FontWeight.bold),
),
trailing: Text(
jsonlist[index]["userId"].toString(),
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0),
),
),
);
},
),
);
}
}



Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter