Second API Calling with Cubit in Flutter

 import "dart:convert";

import "package:api_with_cubit/1st_API_call_using_Cubit/Model/users_details_model.dart";
import "package:http/http.dart" as http;

class ApiServices {
/// 1st way to call API
static Future<UsersDetailsModels?> getDetails() async {
var response = await http.get(
Uri.parse("https://dummyjson.com/users"),
);
if (response.statusCode == 200) {
UsersDetailsModels usersDetailsModels =
UsersDetailsModels.fromJson(jsonDecode(response.body));
return usersDetailsModels;
}
return UsersDetailsModels();
}

/// 2nd way to call API
// static Future<UsersDetailsModels?> getDetails() async {
// var response = await http.get(Uri.parse("https://dummyjson.com/users"));
// if (response.statusCode == 200) {
// return UsersDetailsModels.fromJson(jsonDecode(response.body));
// }
// return null;
// }
}
import 'package:api_with_cubit/1st_API_call_using_Cubit/API_services/api_services.dart';
import 'package:api_with_cubit/1st_API_call_using_Cubit/Model/users_details_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

part 'user_details_state.dart';

class UserDetailsCubit extends Cubit<UserDetailsState> {
UserDetailsCubit() : super(UserDetailsInitial());

Future<void> getDetails() async {
try {
emit(UserDetailsLoading());
final details = await ApiServices.getDetails();
if (details != null) {
print("Data fetched successfully: ${details.users?.length}");
emit(UserDetailsLoaded(usersDetailsModels: details));
} else {
print("Failed to fetch details");
emit(UserDetailsError(errormsg: "Failed to fetch details"));
}
} catch (e) {
print("Error fetching details: $e");
emit(UserDetailsError(errormsg: e.toString()));
}
}
}

part of 'user_details_cubit.dart';

@immutable
sealed class UserDetailsState {}

final class UserDetailsInitial extends UserDetailsState {}

class UserDetailsLoading extends UserDetailsState {}

class UserDetailsLoaded extends UserDetailsState {
final UsersDetailsModels usersDetailsModels;

UserDetailsLoaded({required this.usersDetailsModels});
}

class UserDetailsError extends UserDetailsState {
final String errormsg;

UserDetailsError({required this.errormsg});
}


class UsersDetailsModels {
List<Users>? users;
int? total;
int? skip;
int? limit;

UsersDetailsModels({this.users, this.total, this.skip, this.limit});

UsersDetailsModels.fromJson(Map<String, dynamic> json) {
if (json['users'] != null) {
users = <Users>[];
json['users'].forEach((v) {
users!.add(Users.fromJson(v));
});
}
total = json['total'];
skip = json['skip'];
limit = json['limit'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.users != null) {
data['users'] = this.users!.map((v) => v.toJson()).toList();
}
data['total'] = this.total;
data['skip'] = this.skip;
data['limit'] = this.limit;
return data;
}
}

class Users {
int? id;
String? firstName;
String? lastName;
String? maidenName;
int? age;
String? gender;
String? email;
String? phone;
String? username;
String? password;
String? birthDate;
String? image;
String? bloodGroup;
double? height;
dynamic? weight;
String? eyeColor;
Hair? hair;
String? ip;
Address? address;
String? macAddress;
String? university;
Bank? bank;
Company? company;
String? ein;
String? ssn;
String? userAgent;
Crypto? crypto;
String? role;

Users(
{this.id,
this.firstName,
this.lastName,
this.maidenName,
this.age,
this.gender,
this.email,
this.phone,
this.username,
this.password,
this.birthDate,
this.image,
this.bloodGroup,
this.height,
this.weight,
this.eyeColor,
this.hair,
this.ip,
this.address,
this.macAddress,
this.university,
this.bank,
this.company,
this.ein,
this.ssn,
this.userAgent,
this.crypto,
this.role});

Users.fromJson(Map<String, dynamic> json) {
id = json['id'];
firstName = json['firstName'];
lastName = json['lastName'];
maidenName = json['maidenName'];
age = json['age'];
gender = json['gender'];
email = json['email'];
phone = json['phone'];
username = json['username'];
password = json['password'];
birthDate = json['birthDate'];
image = json['image'];
bloodGroup = json['bloodGroup'];
height = json['height'];
weight = json['weight'];
eyeColor = json['eyeColor'];
hair = json['hair'] != null ? Hair.fromJson(json['hair']) : null;
ip = json['ip'];
address =
json['address'] != null ? Address.fromJson(json['address']) : null;
macAddress = json['macAddress'];
university = json['university'];
bank = json['bank'] != null ? Bank.fromJson(json['bank']) : null;
company =
json['company'] != null ? Company.fromJson(json['company']) : null;
ein = json['ein'];
ssn = json['ssn'];
userAgent = json['userAgent'];
crypto = json['crypto'] != null ? Crypto.fromJson(json['crypto']) : null;
role = json['role'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['firstName'] = this.firstName;
data['lastName'] = this.lastName;
data['maidenName'] = this.maidenName;
data['age'] = this.age;
data['gender'] = this.gender;
data['email'] = this.email;
data['phone'] = this.phone;
data['username'] = this.username;
data['password'] = this.password;
data['birthDate'] = this.birthDate;
data['image'] = this.image;
data['bloodGroup'] = this.bloodGroup;
data['height'] = this.height;
data['weight'] = this.weight;
data['eyeColor'] = this.eyeColor;
if (this.hair != null) {
data['hair'] = this.hair!.toJson();
}
data['ip'] = this.ip;
if (this.address != null) {
data['address'] = this.address!.toJson();
}
data['macAddress'] = this.macAddress;
data['university'] = this.university;
if (this.bank != null) {
data['bank'] = this.bank!.toJson();
}
if (this.company != null) {
data['company'] = this.company!.toJson();
}
data['ein'] = this.ein;
data['ssn'] = this.ssn;
data['userAgent'] = this.userAgent;
if (this.crypto != null) {
data['crypto'] = this.crypto!.toJson();
}
data['role'] = this.role;
return data;
}
}

class Hair {
String? color;
String? type;

Hair({this.color, this.type});

Hair.fromJson(Map<String, dynamic> json) {
color = json['color'];
type = json['type'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['color'] = this.color;
data['type'] = this.type;
return data;
}
}

class Address {
String? address;
String? city;
String? state;
String? stateCode;
String? postalCode;
Coordinates? coordinates;
String? country;

Address(
{this.address,
this.city,
this.state,
this.stateCode,
this.postalCode,
this.coordinates,
this.country});

Address.fromJson(Map<String, dynamic> json) {
address = json['address'];
city = json['city'];
state = json['state'];
stateCode = json['stateCode'];
postalCode = json['postalCode'];
coordinates = json['coordinates'] != null
? Coordinates.fromJson(json['coordinates'])
: null;
country = json['country'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['address'] = this.address;
data['city'] = this.city;
data['state'] = this.state;
data['stateCode'] = this.stateCode;
data['postalCode'] = this.postalCode;
if (this.coordinates != null) {
data['coordinates'] = this.coordinates!.toJson();
}
data['country'] = this.country;
return data;
}
}

class Coordinates {
double? lat;
double? lng;

Coordinates({this.lat, this.lng});

Coordinates.fromJson(Map<String, dynamic> json) {
lat = json['lat'];
lng = json['lng'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['lat'] = this.lat;
data['lng'] = this.lng;
return data;
}
}

class Bank {
String? cardExpire;
String? cardNumber;
String? cardType;
String? currency;
String? iban;

Bank(
{this.cardExpire,
this.cardNumber,
this.cardType,
this.currency,
this.iban});

Bank.fromJson(Map<String, dynamic> json) {
cardExpire = json['cardExpire'];
cardNumber = json['cardNumber'];
cardType = json['cardType'];
currency = json['currency'];
iban = json['iban'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['cardExpire'] = this.cardExpire;
data['cardNumber'] = this.cardNumber;
data['cardType'] = this.cardType;
data['currency'] = this.currency;
data['iban'] = this.iban;
return data;
}
}

class Company {
String? department;
String? name;
String? title;
Address? address;

Company({this.department, this.name, this.title, this.address});

Company.fromJson(Map<String, dynamic> json) {
department = json['department'];
name = json['name'];
title = json['title'];
address =
json['address'] != null ? Address.fromJson(json['address']) : null;
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['department'] = this.department;
data['name'] = this.name;
data['title'] = this.title;
if (this.address != null) {
data['address'] = this.address!.toJson();
}
return data;
}
}

class Crypto {
String? address;
String? city;
String? state;
String? stateCode;
String? postalCode;
Coordinates? coordinates;
String? country;

Crypto(
{this.address,
this.city,
this.state,
this.stateCode,
this.postalCode,
this.coordinates,
this.country});

Crypto.fromJson(Map<String, dynamic> json) {
address = json['address'];
city = json['city'];
state = json['state'];
stateCode = json['stateCode'];
postalCode = json['postalCode'];
coordinates = json['coordinates'] != null
? Coordinates.fromJson(json['coordinates'])
: null;
country = json['country'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['address'] = this.address;
data['city'] = this.city;
data['state'] = this.state;
data['stateCode'] = this.stateCode;
data['postalCode'] = this.postalCode;
if (this.coordinates != null) {
data['coordinates'] = this.coordinates!.toJson();
}
data['country'] = this.country;
return data;
}
}


import "package:api_with_cubit/1st_API_call_using_Cubit/cubits/user_details_cubit.dart";
import "package:flutter/material.dart";
import "package:flutter_bloc/flutter_bloc.dart";

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

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

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) {
return UserDetailsCubit();
},
child: MaterialApp(
title: "HomeScreen",
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: const AppBarTheme(
backgroundColor: Colors.grey,
centerTitle: true,
),
),
home: const HomeScreen(),
),
);
}
}

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

@override
State<HomeScreen> createState() {
return HomeScreenState();
}
}

class HomeScreenState extends State<HomeScreen> {
@override
void initState() {
context.read<UserDetailsCubit>().getDetails();
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"HomeScreen",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
body: BlocConsumer<UserDetailsCubit, UserDetailsState>(
listener: (context, state) {
return print(state);
},
builder: (context, state) {
if (state is UserDetailsLoading) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (state is UserDetailsError) {
return Center(
child: Text(
state.errormsg,
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0),
),
);
} else if (state is UserDetailsLoaded) {
return ListView.builder(
shrinkWrap: true,
itemCount: state.usersDetailsModels.users!.length,
itemBuilder: (context, index) {
final data = state.usersDetailsModels.users![index];
print(data);
return Card(
child: ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(data.image.toString()),
),
title: Text(
data.firstName.toString(),
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(data.hair!.color.toString()),
),
);
},
);
}
return Container();
},
),
);
}
}




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