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

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

@override
State<EighthDio> createState() {
return EighthDioState();
}
}

class EighthDioState extends State<EighthDio> {
var jsonlist = [];

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

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

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(
"Eighth 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: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Title : ${jsonlist[index]["title"]}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10.0),
Text("completed : ${jsonlist[index]["completed"]}"),
],
),
trailing: Text(
jsonlist[index]["userId"].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