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

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

@override
State<FirstDio> createState() {
return FirstDioState();
}
}

class FirstDioState extends State<FirstDio> {
var jsonlist = [];

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

void getData() async {
try {
var response = await Dio().get("https://fakestoreapi.com/products");
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(
"First 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: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
jsonlist[index]["id"].toString(),
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text(
jsonlist[index]["title"],
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(jsonlist[index]["description"]),
const SizedBox(height: 10.0),
Text(jsonlist[index]["category"]),
const SizedBox(height: 10.0),
Text(
jsonlist[index]["image"],
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text("Rate : ${jsonlist[index]["rating"]["rate"]}"),
Text("Count : ${jsonlist[index]["rating"]["count"]}"),
],
),
trailing: Text(
jsonlist[index]["price"].toString(),
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0),
),
),
);
},
),
);
}
}



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