How to convert string arraylist to model arraylist in flutter? - flutter

List<String> langList = [English, Gujarati, Hindi, Marathi, Punjabi, Urdu, Spanish]
var selectedLanguagesList = <LanguageDatum>[].obs;
langList is a string type of list. and selectedLanguagesList is a model type of list.
How do I convert string list to model list in flutter ?
class LanguageDatum {
LanguageDatum({
this.id,
this.name,
this.status,
this.createdAt,
this.updatedAt,
this.deletedAt,
});
int? id;
String? name;
int? status;
DateTime? createdAt;
DateTime? updatedAt;
dynamic deletedAt;
factory LanguageDatum.fromJson(Map<String, dynamic> json) => LanguageDatum(
id: json["id"]??0,
name: json["name"]??"",
status: json["status"]??"",
createdAt: json["created_at"] != null ? DateTime.parse(json["created_at"]) : null,
updatedAt: json["updated_at"] != null ? DateTime.parse(json["updated_at"]) : null,
deletedAt: json["deleted_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"status": status,
"created_at": createdAt,
"updated_at": updatedAt,
"deleted_at": deletedAt,
};
}
So, Above is model class.

If I understand your question correctly, I guess it would be
// To convert String array to Model array
final listOfLanguageDatum =
langList.map((e) => LanguageDatum(name: e)).toList();
List<String> langList = [
'Tamil'
'English',
'Gujarati',
'Hindi',
'Marathi',
'Punjabi',
'Urdu',
'Spanish'
];
class LanguageDatum extends GetxController {
LanguageDatum({
this.id,
this.name,
this.status,
this.createdAt,
this.updatedAt,
this.deletedAt,
});
int? id;
String? name;
int? status;
DateTime? createdAt;
DateTime? updatedAt;
dynamic deletedAt;
factory LanguageDatum.fromJson(Map<String, dynamic> json) => LanguageDatum(
id: json["id"] ?? 0,
name: json["name"] ?? "",
status: json["status"] ?? "",
createdAt: json["created_at"] != null
? DateTime.parse(json["created_at"])
: null,
updatedAt: json["updated_at"] != null
? DateTime.parse(json["updated_at"])
: null,
deletedAt: json["deleted_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"status": status,
"created_at": createdAt,
"updated_at": updatedAt,
"deleted_at": deletedAt,
};
#override
String toString() {
return 'LanguageDatum(id: $id, name: $name, status: $status, createdAt: $createdAt, updateAt: $updatedAt, deletedAt: $deletedAt)';
}
}

Right now your language is of String type, to make language object You need to make a model class say,
Class LanguageDatum{
String name;
...
}

Related

Error "The argument type 'DateTime?' can't be assigned to the parameter type 'DateTime'." flutter firestore

I tried to fetch users'Users data from Firebase Firestore. For that I created a model class.And also in those data has a birthday parameter I tried to define that in model but show this error "The argument type 'DateTime?' can't be assigned to the parameter type 'DateTime'"
My code:
import 'dart:convert';
Users UsersFromJson(String str) => Users.fromJson(json.decode(str));
String UsersToJson(Users data) => json.encode(data.toJson());
class Users {
Users({
required this.id,
required this.url,
required this.name,
required this.birthday,
});
String id;
String name;
String url;
DateTime birthday;
factory Users.fromJson(Map<String, dynamic> json) => Users(
id: json["id"] ?? "",
name: json["name"] ?? "",
url: json["url"] ?? "",
birthday: json["birthday"] != null ? DateTime.parse(json["birthday"]) : null,
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"url": url,
"birthday": birthday?.toString(),
};
}
You can choose one of two codes:
class Users {
Users({
required this.id,
required this.url,
required this.name,
required this.birthday,
});
String id;
String name;
String url;
DateTime? birthday;
factory Users.fromJson(Map<String, dynamic> json) => Users(
id: json["id"] ?? "",
name: json["name"] ?? "",
url: json["url"] ?? "",
birthday: json["birthday"] != null ? DateTime.parse(json["birthday"]) : null,
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"url": url,
"birthday": birthday?.toString(),
};
}
class Users {
Users({
required this.id,
required this.url,
required this.name,
required this.birthday,
});
String id;
String name;
String url;
DateTime birthday;
factory Users.fromJson(Map<String, dynamic> json) => Users(
id: json["id"] ?? "",
name: json["name"] ?? "",
url: json["url"] ?? "",
birthday: json["birthday"] != null ? DateTime.parse(json["birthday"]) : DateTime.now(),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"url": url,
"birthday": birthday?.toString(),
};
}
However, I recommend that you use the first code more, because if the birthday field is null, you can show the user that there is no data.
If you use the second code, I don't think there is any way to check if the birthday field is empty.
Make your birthday nullable. So replace
DateTime birthday;
with
DateTime? birthday;
If you don't want it to be nullable you could instead put a non-null fallback like DateTime.now() for example like
birthday:
json["birthday"] != null ? DateTime.parse(json["birthday"]) : DateTime.now(),
2 work arounds
pass non nullable value to birthday:
birthday: DateTime.parse(json["birthday"]) ?? DateTime.now(),
make birthday nullable
DateTime? birthday
with this method you can keep the existing line of the code. Your error will no longer display.

How to convert model type of list to array in flutter?

I have model type of list, That list is used to collect id and speciality of user.
specialityList is a list.
var specialityList = <SpecialityDatum>[].obs;
SpecialityDatum is Model,
class SpecialityDatum {
SpecialityDatum({
this.id,
this.speciality,
this.status,
this.createdAt,
this.updatedAt,
this.deletedAt,
});
int? id;
String? speciality;
int? status;
DateTime? createdAt;
DateTime? updatedAt;
dynamic deletedAt;
factory SpecialityDatum.fromJson(Map<String, dynamic> json) => SpecialityDatum(
id: json["id"]??0,
speciality: json["speciality"]??"",
status: json["status"]??"",
createdAt: json["created_at"] != null ? DateTime.parse(json["created_at"]) : null,
updatedAt: json["updated_at"] != null ? DateTime.parse(json["updated_at"]) : null,
deletedAt: json["deleted_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"speciality": speciality,
"status": status,
"created_at": createdAt,
"updated_at": updatedAt,
"deleted_at": deletedAt,
};
String toStringView(){ // what ever you name it
return '$speciality'; // if you want to show id and speciality as string
}
}
I want to convert that model list into array, like below,...
Output : [{"speciality_id":12,"speciality_name":"orthopedic"},{"speciality_id":13,"speciality_name":"hyoerthing"}]
Try this:
var result = specialityList.map((e) => e.toJson()).toList();
result would be a list of map that contain Speciality like id, status and other.
there is an easy and short way to do this is:
spouse you have out put in list of map:
var output=[{"speciality_id":12,"speciality_name":"orthopedic"},{"speciality_id":13,"speciality_name":"hyoerthing"}]
var listofObj= output.map((e) => e.toJson()).toList();
Try this
class SpecialityDatum {
SpecialityDatum({
this.id,
this.speciality,
this.status,
this.createdAt,
this.updatedAt,
this.deletedAt,
});
int? id;
String? speciality;
int? status;
DateTime? createdAt;
DateTime? updatedAt;
dynamic deletedAt;
factory SpecialityDatum.fromJson(Map<String, dynamic> json) => SpecialityDatum(
id: json["id"]??0,
speciality: json["speciality"]??"",
status: json["status"]??"",
createdAt: json["created_at"] != null ? DateTime.parse(json["created_at"]) : null,
updatedAt: json["updated_at"] != null ? DateTime.parse(json["updated_at"]) : null,
deletedAt: json["deleted_at"],
);
Map<String, dynamic> toJson() => {
if(id != null) "speciality_id": id, // changed
if(speciality != null) "speciality_name": speciality, // changed
if(status != null) "status": status, // changed
if(createdAt != null) "created_at": createdAt, // changed
if(updatedAt != null) "updated_at": updatedAt, // changed
if(deletedAt != null) "deleted_at": deletedAt, // changed
};
String toStringView(){ // what ever you name it
return '$speciality'; // if you want to show id and speciality as string
}
}
and then you need to map it to new list like:
var mapedSpecialityList = specialityList.map((e) => e.toJson()).toList();

I got an error while getting data from the API, how should I get the data if flutter?

I am getting data from an API using a model. But I ran into a problem that when I get the 'gallery' data, I get an error, that is, I get the data incorrectly. I need to get the 'gallery' field and inside it take the 'url' field - a link to the photo, in order to use it in the future. Can you tell me how to get the 'url' field correctly?
{
"data": {
"id": 35,
"picture_url": null,
"email_confirmed": false,
"gallery": [
{
"url": "https://picture-staging.s3.eu-central.jpeg",
"mime_type": "image/jpeg",
"type": "gallery",
"updated_at": "2022",
"created_at": "2022"
}
],
"updated_at": "2022",
"created_at": "2022"
}
}
model
class User {
final int id;
List? gallery;
User({
required this.id,
this.gallery,
});
User.fromJson(Map<String, dynamic> json)
: this(
id: json['id'] as int,
gallery: json['gallery']['url'],
);
In your API response, there is a list of gallery objects therefore you have to traverse through all of them.
User.fromJson(Map<String, dynamic> json) {
json = json['data'];
id = json['id'];
pictureUrl = json['picture_url'];
emailConfirmed = json['email_confirmed'];
if (json['gallery'] != null) {
gallery = <Gallery>[];
json['gallery'].forEach((v) {
gallery!.add(new Gallery.fromJson(v));
});
}
updatedAt = json['updated_at'];
createdAt = json['created_at'];
}
There are multiple tools that helps you create that .fromJson method, like this. Paste your json there and it will generate dart code for you, really helps me.
The usage should like this:
User user = User.fromJson(yourApiResponseJson);
print(user.id);
print(user.gallery); //prints entire list of gallery
print(user.gallery.first.url); //prints only first object url
I hope that is not your whole model, because that model is not accessing the "data" key on the json response, your model should start getting the key data then pass it to another class that in this case should be named User
here is a brief example
class User {
User({
required this.data,
});
final Data data;
factory User.fromJson(Map<String, dynamic> json) => User(
data: Data.fromJson(json["data"]),
);
}
The Data class could be like this:
class Data {
Data({
required this.id,
required this.pictureUrl,
required this.emailConfirmed,
required this.gallery,
required this.updatedAt,
required this.createdAt,
});
final int id;
final dynamic pictureUrl;
final bool emailConfirmed;
final List<Gallery> gallery;
final String updatedAt;
final String createdAt;
factory Data.fromJson(Map<String, dynamic> json) => Data(
id: json["id"],
pictureUrl: json["picture_url"],
emailConfirmed: json["email_confirmed"],
gallery: List<Gallery>.from(json["gallery"].map((x) => Gallery.fromJson(x))),
updatedAt: json["updated_at"],
createdAt: json["created_at"],
);
}
I reccomend you using Quicktype
Hey you can use this tool to generate your dart model from json.
Below is generated code from above tool
// final user = userFromJson(jsonString);
import 'dart:convert';
User userFromJson(String str) => User.fromJson(json.decode(str));
String userToJson(User data) => json.encode(data.toJson());
class User {
User({
required this.data,
});
Data data;
factory User.fromJson(Map<String, dynamic> json) => User(
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"data": data.toJson(),
};
}
class Data {
Data({
this.id,
this.pictureUrl,
this.emailConfirmed,
this.gallery,
this.updatedAt,
this.createdAt,
});
int? id;
String? pictureUrl;
bool? emailConfirmed;
List<Gallery>? gallery;
String? updatedAt;
String? createdAt;
factory Data.fromJson(Map<String, dynamic> json) => Data(
id: json["id"],
pictureUrl: json["picture_url"],
emailConfirmed: json["email_confirmed"],
gallery: List<Gallery>.from(json["gallery"].map((x) => Gallery.fromJson(x))),
updatedAt: json["updated_at"],
createdAt: json["created_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"picture_url": pictureUrl,
"email_confirmed": emailConfirmed,
"gallery": List<dynamic>.from(gallery.map((x) => x.toJson())),
"updated_at": updatedAt,
"created_at": createdAt,
};
}
class Gallery {
Gallery({
this.url,
this.mimeType,
this.type,
this.updatedAt,
this.createdAt,
});
String? url;
String? mimeType;
String? type;
String? updatedAt;
String? createdAt;
factory Gallery.fromJson(Map<String, dynamic> json) => Gallery(
url: json["url"],
mimeType: json["mime_type"],
type: json["type"],
updatedAt: json["updated_at"],
createdAt: json["created_at"],
);
Map<String, dynamic> toJson() => {
"url": url,
"mime_type": mimeType,
"type": type,
"updated_at": updatedAt,
"created_at": createdAt,
};
}
// You can use like this
final user = userFromJson(jsonString);
String? url = user.data?.gallery?.url;

How do I display user's profile in flutter?

I am fetching one row from a database and sending it to flutter where I decode it to receive the following response using var userProfile = json.decode(response.body);
[{id: 1, first_name: First, last_name: Last, name: david, email: david4001#gmail.com, phone_number: 12, user_image: null, email_verification_code: null, email_verification_time: null, created_at: 2022-03-24T17:37:17.000000Z, updated_at: 2022-03-29T07:16:25.000000Z}]
I have a UserProfile class
class UserProfile {
final int id;
final String firstName;
final String lastName;
final String email;
final String phoneNumber;
UserProfile({
required this.id,
required this.firstName,
required this.lastName,
required this.email,
required this.phoneNumber,
});
factory UserProfile.fromJson(Map<String, dynamic> json) {
return UserProfile(
id: json["id"],
firstName: json["first_name"],
lastName: json["first_name"],
email: json["email"],
phoneNumber: json["phone_number"],
);
}
}
I am using the following code to find a suitable way to display the data
UserProfile? userProfile;
if (response.statusCode == 200) {
var userProfile = json.decode(response.body);
List<UserProfile> myProfile = [];
for (var k in userProfile) {
myProfile.add(UserProfile.fromJson(userProfile));
}
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load user data');
}
I am getting the error below
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
How do I handle the error?
You are passing whole list instead of value.
Try this.
var userProfile = json.decode(response.body);
List<UserProfile> myProfile = [];
for (var k in userProfile) {
myProfile.add(UserProfile.fromJson(k));
}
You will try like this way
const myJson = """
[
{
"id": 1,
"first_name": "First",
"last_name": "Last",
"name": "david",
"email": "david4001#gmail.com",
"phone_number": "12",
"user_image": null,
"email_verification_code": null,
"email_verification_time": null,
"created_at": "2022-03-24T17:37:17.000000Z",
"updated_at": "2022-03-29T07:16:25.000000Z"
}
]
""";
class UserProfile {
UserProfile({
this.id,
this.firstName,
this.lastName,
this.name,
this.email,
this.phoneNumber,
this.userImage,
this.emailVerificationCode,
this.emailVerificationTime,
this.createdAt,
this.updatedAt,
});
int? id;
String? firstName;
String? lastName;
String? name;
String? email;
String? phoneNumber;
dynamic userImage;
dynamic emailVerificationCode;
dynamic emailVerificationTime;
DateTime? createdAt;
DateTime? updatedAt;
factory UserProfile.fromMap(Map<String, dynamic> json) => UserProfile(
id: json["id"],
firstName: json["first_name"],
lastName: json["last_name"],
name: json["name"],
email: json["email"],
phoneNumber: json["phone_number"],
userImage: json["user_image"],
emailVerificationCode: json["email_verification_code"],
emailVerificationTime: json["email_verification_time"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
);
}
void main() {
final userData = List.from(json.decode(myJson));
print(userData[0]['id']);
print(userData[0]['name']);
}

json response with model not returns any data flutter

So i am trying to retrieve json to a model,but i am getting response but when pass the response.body it shows nothing and it throws error but i am not sure what is wrong,when i try to debug this but it doesn't show anything and i have put this in try catch it shows
Invalid argument(s) (input): Must not be null error
Function for fetch data
var response = await http.get(Uri.parse(url), headers: {
"Content-Type": "application/json",
"Authorization": "Bearer $tokenData",
"Accept": "application/json"
});
print('GetWorkingLists Response status: ${response.statusCode}');
print('GetWorkingLists Response body: ${response.body}');
setState(() {
final deleteModel = deleteModelFromJson(response.body);
print(deleteModel.data.vehicleRegisters.data[0].registerNumber);
});
json response
{
"success": true,
"message": "",
"data": {
"vehicle_registers": {
"current_page": 1,
"data": [
{
"id": 828,
"station_id": 3,
"branch_id": 43,
"manager_id": null,
"employee_id": null,
"created_by": 43,
"vehicle_id": 126,
"customer_name": "Test 31",
"mobile": "156656516",
"location": "loction",
"register_number": "KL12",
"working_status_id": 2,
"working_status_created_at": "2021-12-15 12:05:00",
"remarks": null,
"amount": "700.00",
"extra_amount": "0.00",
"discount": "0.00",
"total_amount": "700.00",
"suggestion": "test",
"inspection_comment": "test",
"feedback": null,
"app_or_web": 2,
"created_at": "15-12-2021 11:44 am",
"updated_at": "2021-12-15 12:05:00",
"deleted_at": null,
"working_status": {
"id": 2,
"name": "Work completed"
},
"vehicle": {
"id": 126,
"name": "Hyundai Creta"
},
"vehicle_registers_services": [
{
"id": 2317,
"vehicle_registers_id": 828,
"service_type_id": 1,
"service_type": {
"id": 1,
"name": "Car Wash"
},
"vehicle_types_serives_prices": {
"service_type_id": 1,
"price": "200.00"
}
},
{
"id": 2318,
"vehicle_registers_id": 828,
"service_type_id": 2,
"service_type": {
"id": 2,
"name": "Full Body Wash"
},
"vehicle_types_serives_prices": {
"service_type_id": 2,
"price": "500.00"
}
}
],
"vehicle_registers_accessories": [],
"vehicle_registers_statuses": [
{
"id": 1618,
"vehicle_registers_id": 828,
"working_status_id": 0,
"created_at": "15-12-2021 11:44 am",
"working_status": {
"id": 0,
"name": "Registered"
}
},
{
"id": 1619,
"vehicle_registers_id": 828,
"working_status_id": 1,
"created_at": "15-12-2021 11:44 am",
"working_status": {
"id": 1,
"name": "Work started"
}
},
{
"id": 1620,
"vehicle_registers_id": 828,
"working_status_id": 2,
"created_at": "15-12-2021 12:05 pm",
"working_status": {
"id": 2,
"name": "Work completed"
}
}
],
"vehicle_registers_used_stocks": [
{
"id": 16,
"vehicle_registers_id": 828,
"stocks_station_id": 31,
"station_id": 3,
"branch_id": 43,
"stock_id": 11,
"unit": 1,
"kg": 10,
"gm": 5,
"ltr": 0,
"ml": 0,
"counter": 0,
"created_at": "2021-12-15 12:05:00",
"updated_at": null,
"deleted_at": null,
"stocks_station": {
"id": 31,
"stock_id": 11,
"station_id": 3,
"branch_id": 43,
"kg": 0,
"gm": 0,
"ltr": 0,
"ml": 0,
"counter": 0,
"created_at": "2021-12-14 10:50:06",
"updated_at": "2021-12-15 12:05:00",
"deleted_at": null,
"stock": {
"id": 11,
"station_id": 3,
"name": "Soap Powder",
"unit": 1,
"stock_date": "2021-12-14",
"created_at": "2021-12-07 16:28:42",
"updated_at": "2021-12-14 10:50:06",
"deleted_at": null
}
}
}
],
"service_station": {
"id": 3,
"name": "Station 01"
},
"employee": null,
"created_byy": {
"id": 43,
"name": "Branch1"
}
}
],
"first_page_url": "http:\/\/165.22.222.162\/api\/vehicle-register?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http:\/\/165.22.222.162\/api\/vehicle-register?page=1",
"next_page_url": null,
"path": "http:\/\/165.22.222.162\/api\/vehicle-register",
"per_page": 10,
"prev_page_url": null,
"to": 1,
"total": 1
},
"total_amount": 700
}
}
Model
// To parse this JSON data, do
//
// final deleteModel = deleteModelFromJson(jsonString);
import 'dart:convert';
DeleteModel deleteModelFromJson(String str) => DeleteModel.fromJson(json.decode(str));
String deleteModelToJson(DeleteModel data) => json.encode(data.toJson());
class DeleteModel {
DeleteModel({
this.success,
this.message,
this.data,
});
bool success;
String message;
Data data;
factory DeleteModel.fromJson(Map<String, dynamic> json) => DeleteModel(
success: json["success"],
message: json["message"],
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"success": success,
"message": message,
"data": data.toJson(),
};
}
class Data {
Data({
this.vehicleRegisters,
this.totalAmount,
});
VehicleRegisters vehicleRegisters;
int totalAmount;
factory Data.fromJson(Map<String, dynamic> json) => Data(
vehicleRegisters: VehicleRegisters.fromJson(json["vehicle_registers"]),
totalAmount: json["total_amount"],
);
Map<String, dynamic> toJson() => {
"vehicle_registers": vehicleRegisters.toJson(),
"total_amount": totalAmount,
};
}
class VehicleRegisters {
VehicleRegisters({
this.currentPage,
this.data,
this.firstPageUrl,
this.from,
this.lastPage,
this.lastPageUrl,
this.nextPageUrl,
this.path,
this.perPage,
this.prevPageUrl,
this.to,
this.total,
});
int currentPage;
List<Datum> data;
String firstPageUrl;
int from;
int lastPage;
String lastPageUrl;
dynamic nextPageUrl;
String path;
int perPage;
dynamic prevPageUrl;
int to;
int total;
factory VehicleRegisters.fromJson(Map<String, dynamic> json) => VehicleRegisters(
currentPage: json["current_page"],
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
firstPageUrl: json["first_page_url"],
from: json["from"],
lastPage: json["last_page"],
lastPageUrl: json["last_page_url"],
nextPageUrl: json["next_page_url"],
path: json["path"],
perPage: json["per_page"],
prevPageUrl: json["prev_page_url"],
to: json["to"],
total: json["total"],
);
Map<String, dynamic> toJson() => {
"current_page": currentPage,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
"first_page_url": firstPageUrl,
"from": from,
"last_page": lastPage,
"last_page_url": lastPageUrl,
"next_page_url": nextPageUrl,
"path": path,
"per_page": perPage,
"prev_page_url": prevPageUrl,
"to": to,
"total": total,
};
}
class Datum {
Datum({
this.id,
this.stationId,
this.branchId,
this.managerId,
this.employeeId,
this.createdBy,
this.vehicleId,
this.customerName,
this.mobile,
this.location,
this.registerNumber,
this.workingStatusId,
this.workingStatusCreatedAt,
this.remarks,
this.amount,
this.extraAmount,
this.discount,
this.totalAmount,
this.suggestion,
this.inspectionComment,
this.feedback,
this.appOrWeb,
this.createdAt,
this.updatedAt,
this.deletedAt,
this.workingStatus,
this.vehicle,
this.vehicleRegistersServices,
this.vehicleRegistersAccessories,
this.vehicleRegistersStatuses,
this.vehicleRegistersUsedStocks,
this.serviceStation,
this.employee,
this.createdByy,
});
int id;
int stationId;
int branchId;
dynamic managerId;
dynamic employeeId;
int createdBy;
int vehicleId;
String customerName;
String mobile;
String location;
String registerNumber;
int workingStatusId;
DateTime workingStatusCreatedAt;
dynamic remarks;
String amount;
String extraAmount;
String discount;
String totalAmount;
String suggestion;
String inspectionComment;
dynamic feedback;
int appOrWeb;
String createdAt;
DateTime updatedAt;
dynamic deletedAt;
CreatedByy workingStatus;
CreatedByy vehicle;
List<VehicleRegistersService> vehicleRegistersServices;
List<dynamic> vehicleRegistersAccessories;
List<VehicleRegistersStatus> vehicleRegistersStatuses;
List<VehicleRegistersUsedStock> vehicleRegistersUsedStocks;
CreatedByy serviceStation;
dynamic employee;
CreatedByy createdByy;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
id: json["id"],
stationId: json["station_id"],
branchId: json["branch_id"],
managerId: json["manager_id"],
employeeId: json["employee_id"],
createdBy: json["created_by"],
vehicleId: json["vehicle_id"],
customerName: json["customer_name"],
mobile: json["mobile"],
location: json["location"],
registerNumber: json["register_number"],
workingStatusId: json["working_status_id"],
workingStatusCreatedAt: DateTime.parse(json["working_status_created_at"]),
remarks: json["remarks"],
amount: json["amount"],
extraAmount: json["extra_amount"],
discount: json["discount"],
totalAmount: json["total_amount"],
suggestion: json["suggestion"],
inspectionComment: json["inspection_comment"],
feedback: json["feedback"],
appOrWeb: json["app_or_web"],
createdAt: json["created_at"],
updatedAt: DateTime.parse(json["updated_at"]),
deletedAt: json["deleted_at"],
workingStatus: CreatedByy.fromJson(json["working_status"]),
vehicle: CreatedByy.fromJson(json["vehicle"]),
vehicleRegistersServices: List<VehicleRegistersService>.from(json["vehicle_registers_services"].map((x) => VehicleRegistersService.fromJson(x))),
vehicleRegistersAccessories: List<dynamic>.from(json["vehicle_registers_accessories"].map((x) => x)),
vehicleRegistersStatuses: List<VehicleRegistersStatus>.from(json["vehicle_registers_statuses"].map((x) => VehicleRegistersStatus.fromJson(x))),
vehicleRegistersUsedStocks: List<VehicleRegistersUsedStock>.from(json["vehicle_registers_used_stocks"].map((x) => VehicleRegistersUsedStock.fromJson(x))),
serviceStation: CreatedByy.fromJson(json["service_station"]),
employee: json["employee"],
createdByy: CreatedByy.fromJson(json["created_byy"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"station_id": stationId,
"branch_id": branchId,
"manager_id": managerId,
"employee_id": employeeId,
"created_by": createdBy,
"vehicle_id": vehicleId,
"customer_name": customerName,
"mobile": mobile,
"location": location,
"register_number": registerNumber,
"working_status_id": workingStatusId,
"working_status_created_at": workingStatusCreatedAt.toIso8601String(),
"remarks": remarks,
"amount": amount,
"extra_amount": extraAmount,
"discount": discount,
"total_amount": totalAmount,
"suggestion": suggestion,
"inspection_comment": inspectionComment,
"feedback": feedback,
"app_or_web": appOrWeb,
"created_at": createdAt,
"updated_at": updatedAt.toIso8601String(),
"deleted_at": deletedAt,
"working_status": workingStatus.toJson(),
"vehicle": vehicle.toJson(),
"vehicle_registers_services": List<dynamic>.from(vehicleRegistersServices.map((x) => x.toJson())),
"vehicle_registers_accessories": List<dynamic>.from(vehicleRegistersAccessories.map((x) => x)),
"vehicle_registers_statuses": List<dynamic>.from(vehicleRegistersStatuses.map((x) => x.toJson())),
"vehicle_registers_used_stocks": List<dynamic>.from(vehicleRegistersUsedStocks.map((x) => x.toJson())),
"service_station": serviceStation.toJson(),
"employee": employee,
"created_byy": createdByy.toJson(),
};
}
class CreatedByy {
CreatedByy({
this.id,
this.name,
});
int id;
String name;
factory CreatedByy.fromJson(Map<String, dynamic> json) => CreatedByy(
id: json["id"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
};
}
class VehicleRegistersService {
VehicleRegistersService({
this.id,
this.vehicleRegistersId,
this.serviceTypeId,
this.serviceType,
this.vehicleTypesSerivesPrices,
});
int id;
int vehicleRegistersId;
int serviceTypeId;
CreatedByy serviceType;
VehicleTypesSerivesPrices vehicleTypesSerivesPrices;
factory VehicleRegistersService.fromJson(Map<String, dynamic> json) => VehicleRegistersService(
id: json["id"],
vehicleRegistersId: json["vehicle_registers_id"],
serviceTypeId: json["service_type_id"],
serviceType: CreatedByy.fromJson(json["service_type"]),
vehicleTypesSerivesPrices: VehicleTypesSerivesPrices.fromJson(json["vehicle_types_serives_prices"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"vehicle_registers_id": vehicleRegistersId,
"service_type_id": serviceTypeId,
"service_type": serviceType.toJson(),
"vehicle_types_serives_prices": vehicleTypesSerivesPrices.toJson(),
};
}
class VehicleTypesSerivesPrices {
VehicleTypesSerivesPrices({
this.serviceTypeId,
this.price,
});
int serviceTypeId;
String price;
factory VehicleTypesSerivesPrices.fromJson(Map<String, dynamic> json) => VehicleTypesSerivesPrices(
serviceTypeId: json["service_type_id"],
price: json["price"],
);
Map<String, dynamic> toJson() => {
"service_type_id": serviceTypeId,
"price": price,
};
}
class VehicleRegistersStatus {
VehicleRegistersStatus({
this.id,
this.vehicleRegistersId,
this.workingStatusId,
this.createdAt,
this.workingStatus,
});
int id;
int vehicleRegistersId;
int workingStatusId;
String createdAt;
CreatedByy workingStatus;
factory VehicleRegistersStatus.fromJson(Map<String, dynamic> json) => VehicleRegistersStatus(
id: json["id"],
vehicleRegistersId: json["vehicle_registers_id"],
workingStatusId: json["working_status_id"],
createdAt: json["created_at"],
workingStatus: CreatedByy.fromJson(json["working_status"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"vehicle_registers_id": vehicleRegistersId,
"working_status_id": workingStatusId,
"created_at": createdAt,
"working_status": workingStatus.toJson(),
};
}
class VehicleRegistersUsedStock {
VehicleRegistersUsedStock({
this.id,
this.vehicleRegistersId,
this.stocksStationId,
this.stationId,
this.branchId,
this.stockId,
this.unit,
this.kg,
this.gm,
this.ltr,
this.ml,
this.counter,
this.createdAt,
this.updatedAt,
this.deletedAt,
this.stocksStation,
this.stock,
});
int id;
int vehicleRegistersId;
int stocksStationId;
int stationId;
int branchId;
int stockId;
int unit;
int kg;
int gm;
int ltr;
int ml;
int counter;
DateTime createdAt;
DateTime updatedAt;
dynamic deletedAt;
VehicleRegistersUsedStock stocksStation;
Stock stock;
factory VehicleRegistersUsedStock.fromJson(Map<String, dynamic> json) => VehicleRegistersUsedStock(
id: json["id"],
vehicleRegistersId: json["vehicle_registers_id"] == null ? null : json["vehicle_registers_id"],
stocksStationId: json["stocks_station_id"] == null ? null : json["stocks_station_id"],
stationId: json["station_id"],
branchId: json["branch_id"],
stockId: json["stock_id"],
unit: json["unit"] == null ? null : json["unit"],
kg: json["kg"],
gm: json["gm"],
ltr: json["ltr"],
ml: json["ml"],
counter: json["counter"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: json["updated_at"] == null ? null : DateTime.parse(json["updated_at"]),
deletedAt: json["deleted_at"],
stocksStation: json["stocks_station"] == null ? null : VehicleRegistersUsedStock.fromJson(json["stocks_station"]),
stock: json["stock"] == null ? null : Stock.fromJson(json["stock"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"vehicle_registers_id": vehicleRegistersId == null ? null : vehicleRegistersId,
"stocks_station_id": stocksStationId == null ? null : stocksStationId,
"station_id": stationId,
"branch_id": branchId,
"stock_id": stockId,
"unit": unit == null ? null : unit,
"kg": kg,
"gm": gm,
"ltr": ltr,
"ml": ml,
"counter": counter,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt == null ? null : updatedAt.toIso8601String(),
"deleted_at": deletedAt,
"stocks_station": stocksStation == null ? null : stocksStation.toJson(),
"stock": stock == null ? null : stock.toJson(),
};
}
class Stock {
Stock({
this.id,
this.stationId,
this.name,
this.unit,
this.stockDate,
this.createdAt,
this.updatedAt,
this.deletedAt,
});
int id;
int stationId;
String name;
int unit;
DateTime stockDate;
DateTime createdAt;
DateTime updatedAt;
dynamic deletedAt;
factory Stock.fromJson(Map<String, dynamic> json) => Stock(
id: json["id"],
stationId: json["station_id"],
name: json["name"],
unit: json["unit"],
stockDate: DateTime.parse(json["stock_date"]),
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
deletedAt: json["deleted_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"station_id": stationId,
"name": name,
"unit": unit,
"stock_date": "${stockDate.year.toString().padLeft(4, '0')}-${stockDate.month.toString().padLeft(2, '0')}-${stockDate.day.toString().padLeft(2, '0')}",
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
"deleted_at": deletedAt,
};
}
use This Type Model
class Data {
int? id;
int? stationId;
int? branchId;
Null? managerId;
Null? employeeId;
int? createdBy;
int? vehicleId;
String? customerName;
String? mobile;
String? location;
String? registerNumber;
int? workingStatusId;
String? workingStatusCreatedAt;
Null? remarks;
String? amount;
String? extraAmount;
String? discount;
String? totalAmount;
String? suggestion;
String? inspectionComment;
Null? feedback;
int? appOrWeb;
String? createdAt;
String? updatedAt;
Null? deletedAt;
WorkingStatus? workingStatus;
WorkingStatus? vehicle;
List<VehicleRegistersServices>? vehicleRegistersServices;
List<Null>? vehicleRegistersAccessories;
List<VehicleRegistersStatuses>? vehicleRegistersStatuses;
List<VehicleRegistersUsedStocks>? vehicleRegistersUsedStocks;
WorkingStatus? serviceStation;
Null? employee;
WorkingStatus? createdByy;
Data(
{required this.id,
required this.stationId,
required this.branchId,
required this.managerId,
required this.employeeId,
required this.createdBy,
required this.vehicleId,
required this.customerName,
required this.mobile,
required this.location,
required this.registerNumber,
required this.workingStatusId,
required this.workingStatusCreatedAt,
required this.remarks,
required this.amount,
required this.extraAmount,
required this.discount,
required this.totalAmount,
required this.suggestion,
required this.inspectionComment,
required this.feedback,
required this.appOrWeb,
required this.createdAt,
required this.updatedAt,
required this.deletedAt,
required this.workingStatus,
required this.vehicle,
required this.vehicleRegistersServices,
required this.vehicleRegistersAccessories,
required this.vehicleRegistersStatuses,
required this.vehicleRegistersUsedStocks,
required this.serviceStation,
required this.employee,
required this.createdByy});
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
stationId = json['station_id'];
branchId = json['branch_id'];
managerId = json['manager_id'];
employeeId = json['employee_id'];
createdBy = json['created_by'];
vehicleId = json['vehicle_id'];
customerName = json['customer_name'];
mobile = json['mobile'];
location = json['location'];
registerNumber = json['register_number'];
workingStatusId = json['working_status_id'];
workingStatusCreatedAt = json['working_status_created_at'];
remarks = json['remarks'];
amount = json['amount'];
extraAmount = json['extra_amount'];
discount = json['discount'];
totalAmount = json['total_amount'];
suggestion = json['suggestion'];
inspectionComment = json['inspection_comment'];
feedback = json['feedback'];
appOrWeb = json['app_or_web'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
deletedAt = json['deleted_at'];
workingStatus = json['working_status'] != null
? new WorkingStatus.fromJson(json['working_status'])
: null;
vehicle = json['vehicle'] != null
? new WorkingStatus.fromJson(json['vehicle'])
: null;
if (json['vehicle_registers_services'] != null) {
vehicleRegistersServices = <VehicleRegistersServices>[];
json['vehicle_registers_services'].forEach((v) {
vehicleRegistersServices?.add(VehicleRegistersServices.fromJson(v));
});
}
if (json['vehicle_registers_statuses'] != null) {
vehicleRegistersStatuses = <VehicleRegistersStatuses>[];
json['vehicle_registers_statuses'].forEach((v) {
vehicleRegistersStatuses?.add( VehicleRegistersStatuses.fromJson(v));
});
}
if (json['vehicle_registers_used_stocks'] != null) {
vehicleRegistersUsedStocks = <VehicleRegistersUsedStocks>[];
json['vehicle_registers_used_stocks'].forEach((v) {
vehicleRegistersUsedStocks
?.add(new VehicleRegistersUsedStocks.fromJson(v));
});
}
serviceStation = json['service_station'] != null
? new WorkingStatus.fromJson(json['service_station'])
: null;
employee = json['employee'];
createdByy = json['created_byy'] != null
? new WorkingStatus.fromJson(json['created_byy'])
: null;
}
}
class WorkingStatus {
int? id;
String? name;
WorkingStatus({required this.id, required this.name});
WorkingStatus.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
}
class VehicleRegistersServices {
int? id;
int? vehicleRegistersId;
int? serviceTypeId;
WorkingStatus? serviceType;
VehicleTypesSerivesPrices? vehicleTypesSerivesPrices;
VehicleRegistersServices(
{this.id,
this.vehicleRegistersId,
this.serviceTypeId,
this.serviceType,
this.vehicleTypesSerivesPrices});
VehicleRegistersServices.fromJson(Map<String, dynamic> json) {
id = json['id'];
vehicleRegistersId = json['vehicle_registers_id'];
serviceTypeId = json['service_type_id'];
serviceType = json['service_type'] != null
? WorkingStatus.fromJson(json['service_type'])
: null;
vehicleTypesSerivesPrices = json['vehicle_types_serives_prices'] != null
? VehicleTypesSerivesPrices.fromJson(
json['vehicle_types_serives_prices'])
: null;
}
}
class VehicleTypesSerivesPrices {
int? serviceTypeId;
String? price;
VehicleTypesSerivesPrices({required this.serviceTypeId,required this.price});
VehicleTypesSerivesPrices.fromJson(Map<String, dynamic> json) {
serviceTypeId = json['service_type_id'];
price = json['price'];
}
}
class VehicleRegistersStatuses {
int? id;
int? vehicleRegistersId;
int? workingStatusId;
String? createdAt;
WorkingStatus ?workingStatus;
VehicleRegistersStatuses(
{required this.id,
required this.vehicleRegistersId,
required this.workingStatusId,
required this.createdAt,
required this.workingStatus});
VehicleRegistersStatuses.fromJson(Map<String, dynamic> json) {
id = json['id'];
vehicleRegistersId = json['vehicle_registers_id'];
workingStatusId = json['working_status_id'];
createdAt = json['created_at'];
workingStatus = json['working_status'] != null
? new WorkingStatus.fromJson(json['working_status'])
: null;
}
}
class VehicleRegistersUsedStocks {
int? id;
int? vehicleRegistersId;
int? stocksStationId;
int? stationId;
int? branchId;
int? stockId;
int? unit;
int? kg;
int? gm;
int? ltr;
int? ml;
int? counter;
String? createdAt;
Null? updatedAt;
Null? deletedAt;
StocksStation? stocksStation;
VehicleRegistersUsedStocks(
{required this.id,
required this.vehicleRegistersId,
required this.stocksStationId,
required this.stationId,
required this.branchId,
required this.stockId,
required this.unit,
required this.kg,
required this.gm,
required this.ltr,
required this.ml,
required this.counter,
required this.createdAt,
required this.updatedAt,
required this.deletedAt,
required this.stocksStation});
VehicleRegistersUsedStocks.fromJson(Map<String, dynamic> json) {
id = json['id'];
vehicleRegistersId = json['vehicle_registers_id'];
stocksStationId = json['stocks_station_id'];
stationId = json['station_id'];
branchId = json['branch_id'];
stockId = json['stock_id'];
unit = json['unit'];
kg = json['kg'];
gm = json['gm'];
ltr = json['ltr'];
ml = json['ml'];
counter = json['counter'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
deletedAt = json['deleted_at'];
stocksStation = json['stocks_station'] != null
? new StocksStation.fromJson(json['stocks_station'])
: null;
}
}
class StocksStation {
int? id;
int? stockId;
int? stationId;
int? branchId;
int? kg;
int? gm;
int? ltr;
int? ml;
int? counter;
String? createdAt;
String? updatedAt;
void deletedAt;
Stock? stock;
StocksStation(
{required this.id,
required this.stockId,
required this.stationId,
required this.branchId,
required this.kg,
required this.gm,
required this.ltr,
required this.ml,
required this.counter,
required this.createdAt,
required this.updatedAt,
required this.deletedAt,
required this.stock});
StocksStation.fromJson(Map<String, dynamic> json) {
id = json['id'];
stockId = json['stock_id'];
stationId = json['station_id'];
branchId = json['branch_id'];
kg = json['kg'];
gm = json['gm'];
ltr = json['ltr'];
ml = json['ml'];
counter = json['counter'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
deletedAt = json['deleted_at'];
stock = (json['stock'] != null ? new Stock.fromJson(json['stock']) : null)!;
}
}
class Stock {
int? id;
int? stationId;
String? name;
int? unit;
String? stockDate;
String? createdAt;
String? updatedAt;
void deletedAt;
Stock(
{required this.id,
required this.stationId,
required this.name,
required this.unit,
required this.stockDate,
required this.createdAt,
required this.updatedAt,
required this.deletedAt});
Stock.fromJson(Map<String, dynamic> json) {
id = json['id'];
stationId = json['station_id'];
name = json['name'];
unit = json['unit'];
stockDate = json['stock_date'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
deletedAt = json['deleted_at'];
}
}
If you set "Content-Type": "application/json" your response data is a json object and you have a parameter type String in that method: DeleteModel deleteModelFromJson(String str). Change to same type.