The following code is for fetching student data using get API and I am using hive package link is below:
<https://pub.dev/packages/hive>
static Future<void> fetchStudentData() async {
var box = await Hive.openBox('studentDetails');
final pref = await SharedPreferences.getInstance();
try {
print('try block${pref.getString('token')}');
final response = await http.get(
Uri.parse('https:my api url'),
headers: {
'Authorization':
'Bearer ${pref.getString('token')}', // Replace with your actual token
},
);
if (response.statusCode == 200) {
final Map<String, dynamic> data = json.decode(response.body);
print('Saved');
box.put('details', data);
// here i want to store to parse the data into Student model and save it in
box.put('details',parse data)
print('Saved ===${box.get('details')}');
} else {
throw Exception('Failed to load student data');
}
} catch (error) {
throw Exception('Error: $error');
}}
and this is my Student Model
class Student {
List<StudentData>? studentData;
Student({this.studentData});
Student.fromJson(Map<String, dynamic> json) {
if (json['StudentData'] != null) {
studentData = <StudentData>[];
json['StudentData'].forEach((v) {
studentData!.add(StudentData.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.studentData != null) {
data['StudentData'] = this.studentData!.map((v) => v.toJson()).toList();
}
return data;
}
}
class StudentData {
String? formId;
String? campusID;
String? name;
String? fatherName;
String? fatherNIC;
String? fatherOccupation;
String? gender;
String? dOB;
String? religion;
String? photo;
String? domicile;
String? placeOfBirth;
String? nationality;
String? bFormNo;
String? bloodGroup;
String? anuualIncome;
String? motherName;
String? motherNIC;
String? guardianName;
String? motherOccupation;
String? guardianNIC;
String? guardianOccupation;
String? nIC;
String? formEntryDate;
String? busID;
String? transport;
String? hafizQuran;
String? hostelRequired;
String? lefthaded;
String? maritaStatus;
String? disability;
String? description;
String? diseases;
String? stdId;
String? regId;
String? admissionDate;
String? session;
String? leavingDate;
String? mainSection;
String? programName;
String? classID;
String? section;
String? status;
String? shift;
String? scholorShip;
String? scholorshipType;
String? reasonForLeaving;
String? boardRegistrationNo;
String? type;
String? iD;
String? isExpelled;
String? fatherNo;
String? guardianNo;
String? motherNo;
String? homeNo;
String? officeNo;
String? other;
String? sMS1;
String? sMS2;
String? lastClass;
String? area;
String? fatherEducation;
String? motherEducation;
String? guardianEducation;
String? fatherDesignation;
String? urduLevel;
String? guardianDepartment;
String? fatherDepartment;
String? guardianDesignation;
String? concessionDescription;
String? concessionFrom;
String? concessionTo;
String? totalPresents;
String? outOf;
String? conduct;
String? remarks;
String? sLCNo;
StudentData(
{this.formId,
this.campusID,
this.name,
this.fatherName,
this.fatherNIC,
this.fatherOccupation,
this.gender,
this.dOB,
this.religion,
this.photo,
this.domicile,
this.placeOfBirth,
this.nationality,
this.bFormNo,
this.bloodGroup,
this.anuualIncome,
this.motherName,
this.motherNIC,
this.guardianName,
this.motherOccupation,
this.guardianNIC,
this.guardianOccupation,
this.nIC,
this.formEntryDate,
this.busID,
this.transport,
this.hafizQuran,
this.hostelRequired,
this.lefthaded,
this.maritaStatus,
this.disability,
this.description,
this.diseases,
this.stdId,
this.regId,
this.admissionDate,
this.session,
this.leavingDate,
this.mainSection,
this.programName,
this.classID,
this.section,
this.status,
this.shift,
this.scholorShip,
this.scholorshipType,
this.reasonForLeaving,
this.boardRegistrationNo,
this.type,
this.iD,
this.isExpelled,
this.fatherNo,
this.guardianNo,
this.motherNo,
this.homeNo,
this.officeNo,
this.other,
this.sMS1,
this.sMS2,
this.lastClass,
this.area,
this.fatherEducation,
this.motherEducation,
this.guardianEducation,
this.fatherDesignation,
this.urduLevel,
this.guardianDepartment,
this.fatherDepartment,
this.guardianDesignation,
this.concessionDescription,
this.concessionFrom,
this.concessionTo,
this.totalPresents,
this.outOf,
this.conduct,
this.remarks,
this.sLCNo});
StudentData.fromJson(Map<String, dynamic> json) {
formId = json['FormId'];
campusID = json['CampusID'];
name = json['Name'];
fatherName = json['FatherName'];
fatherNIC = json['FatherNIC'];
fatherOccupation = json['FatherOccupation'];
gender = json['Gender'];
dOB = json['DOB'];
religion = json['Religion'];
photo = json['Photo'];
domicile = json['Domicile'];
placeOfBirth = json['PlaceOfBirth'];
nationality = json['Nationality'];
bFormNo = json['B_FormNo'];
bloodGroup = json['BloodGroup'];
anuualIncome = json['AnuualIncome'];
motherName = json['MotherName'];
motherNIC = json['MotherNIC'];
guardianName = json['GuardianName'];
motherOccupation = json['MotherOccupation'];
guardianNIC = json['GuardianNIC'];
guardianOccupation = json['GuardianOccupation'];
nIC = json['NIC'];
formEntryDate = json['FormEntryDate'];
busID = json['BusID'];
transport = json['Transport'];
hafizQuran = json['HafizQuran'];
hostelRequired = json['HostelRequired'];
lefthaded = json['Lefthaded'];
maritaStatus = json['MaritaStatus'];
disability = json['Disability'];
description = json['Description'];
diseases = json['Diseases'];
stdId = json['StdId'];
regId = json['RegId'];
admissionDate = json['AdmissionDate'];
session = json['Session'];
leavingDate = json['LeavingDate'];
mainSection = json['MainSection'];
programName = json['ProgramName'];
classID = json['ClassID'];
section = json['Section'];
status = json['Status'];
shift = json['Shift'];
scholorShip = json['ScholorShip'];
scholorshipType = json['ScholorshipType'];
reasonForLeaving = json['ReasonForLeaving'];
boardRegistrationNo = json['BoardRegistrationNo'];
type = json['Type'];
iD = json['ID'];
isExpelled = json['IsExpelled'];
fatherNo = json['FatherNo'];
guardianNo = json['GuardianNo'];
motherNo = json['MotherNo'];
homeNo = json['HomeNo'];
officeNo = json['OfficeNo'];
other = json['Other'];
sMS1 = json['SMS1'];
sMS2 = json['SMS2'];
lastClass = json['LastClass'];
area = json['Area'];
fatherEducation = json['FatherEducation'];
motherEducation = json['MotherEducation'];
guardianEducation = json['GuardianEducation'];
fatherDesignation = json['FatherDesignation'];
urduLevel = json['UrduLevel'];
guardianDepartment = json['GuardianDepartment'];
fatherDepartment = json['FatherDepartment'];
guardianDesignation = json['GuardianDesignation'];
concessionDescription = json['ConcessionDescription'];
concessionFrom = json['ConcessionFrom'];
concessionTo = json['ConcessionTo'];
totalPresents = json['TotalPresents'];
outOf = json['OutOf'];
conduct = json['Conduct'];
remarks = json['Remarks'];
sLCNo = json['SLCNo'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['FormId'] = this.formId;
data['CampusID'] = this.campusID;
data['Name'] = this.name;
data['FatherName'] = this.fatherName;
data['FatherNIC'] = this.fatherNIC;
data['FatherOccupation'] = this.fatherOccupation;
data['Gender'] = this.gender;
data['DOB'] = this.dOB;
data['Religion'] = this.religion;
data['Photo'] = this.photo;
data['Domicile'] = this.domicile;
data['PlaceOfBirth'] = this.placeOfBirth;
data['Nationality'] = this.nationality;
data['B_FormNo'] = this.bFormNo;
data['BloodGroup'] = this.bloodGroup;
data['AnuualIncome'] = this.anuualIncome;
data['MotherName'] = this.motherName;
data['MotherNIC'] = this.motherNIC;
data['GuardianName'] = this.guardianName;
data['MotherOccupation'] = this.motherOccupation;
data['GuardianNIC'] = this.guardianNIC;
data['GuardianOccupation'] = this.guardianOccupation;
data['NIC'] = this.nIC;
data['FormEntryDate'] = this.formEntryDate;
data['BusID'] = this.busID;
data['Transport'] = this.transport;
data['HafizQuran'] = this.hafizQuran;
data['HostelRequired'] = this.hostelRequired;
data['Lefthaded'] = this.lefthaded;
data['MaritaStatus'] = this.maritaStatus;
data['Disability'] = this.disability;
data['Description'] = this.description;
data['Diseases'] = this.diseases;
data['StdId'] = this.stdId;
data['RegId'] = this.regId;
data['AdmissionDate'] = this.admissionDate;
data['Session'] = this.session;
data['LeavingDate'] = this.leavingDate;
data['MainSection'] = this.mainSection;
data['ProgramName'] = this.programName;
data['ClassID'] = this.classID;
data['Section'] = this.section;
data['Status'] = this.status;
data['Shift'] = this.shift;
data['ScholorShip'] = this.scholorShip;
data['ScholorshipType'] = this.scholorshipType;
data['ReasonForLeaving'] = this.reasonForLeaving;
data['BoardRegistrationNo'] = this.boardRegistrationNo;
data['Type'] = this.type;
data['ID'] = this.iD;
data['IsExpelled'] = this.isExpelled;
data['FatherNo'] = this.fatherNo;
data['GuardianNo'] = this.guardianNo;
data['MotherNo'] = this.motherNo;
data['HomeNo'] = this.homeNo;
data['OfficeNo'] = this.officeNo;
data['Other'] = this.other;
data['SMS1'] = this.sMS1;
data['SMS2'] = this.sMS2;
data['LastClass'] = this.lastClass;
data['Area'] = this.area;
data['FatherEducation'] = this.fatherEducation;
data['MotherEducation'] = this.motherEducation;
data['GuardianEducation'] = this.guardianEducation;
data['FatherDesignation'] = this.fatherDesignation;
data['UrduLevel'] = this.urduLevel;
data['GuardianDepartment'] = this.guardianDepartment;
data['FatherDepartment'] = this.fatherDepartment;
data['GuardianDesignation'] = this.guardianDesignation;
data['ConcessionDescription'] = this.concessionDescription;
data['ConcessionFrom'] = this.concessionFrom;
data['ConcessionTo'] = this.concessionTo;
data['TotalPresents'] = this.totalPresents;
data['OutOf'] = this.outOf;
data['Conduct'] = this.conduct;
data['Remarks'] = this.remarks;
data['SLCNo'] = this.sLCNo;
return data;
}
}
In fetching method after decode the body response how I will convert it model and then it in local database, when I am trying to parse to model it shows two options Student.fromJson
and StudentData.fromJson
. Here I am confused.