I want to toList the json data coming from the API service and list it on FutureBuilder. But no matter what I do, it returns catch. What could be the problem? Json value is coming but I can't use toList property. What should I do about it?
Payment Service :
Future<List<PaymentData>> paymentGetList() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
// ignore: prefer_typing_uninitialized_variables
dynamic responseData;
String token = await getToken();
final paymentListApiUrl = Uri.parse(
apiUrlKey preferences.getString('apiUrl').toString() paymentListUrl);
try {
final response = await http.post(paymentListApiUrl, headers: {
'Authorization': token,
'Accept': 'application/json',
}, body: {
'filters': '',
'params': '',
'limit': '10',
'offset': '0',
'sorting': ''
});
if (response.statusCode == 200) {
var jsonData = jsonDecode(response.body);
var jsonArray = jsonData['data'];
List<PaymentData> jsonResult =
(jsonDecode(response.body)['data'] as List)
.map((e) => PaymentData.fromMap(e))
.toList();
responseData = jsonArray;
}
} catch (e) {
print("Error: $e");
}
return responseData;
}
result print : flutter: Error: type 'Null' is not a subtype of type 'String'
PaymentModel
PaymentData paymentDataFromMap(String str) =>
PaymentData.fromMap(json.decode(str));
String paymentDataToMap(PaymentData data) => json.encode(data.toMap());
class PaymentData {
PaymentData({
required this.id,
required this.paymentsendId,
required this.collectionEmail,
required this.collectionSms,
required this.periodOrder,
required this.email,
required this.cardType1,
required this.cardType2,
required this.cardNameSurname,
required this.comment,
required this.datumNull,
required this.customerId,
required this.orderid,
required this.installmentNumber,
required this.plusInstallment,
required this.status,
required this.maskedCard,
required this.totalWithCommison,
required this.totalWithoutCommison,
required this.currencyType,
required this.transactionData,
required this.transactionBank,
required this.transactionType,
required this.partial,
required this.nameSurname,
required this.phoneNumber,
required this.postCode,
required this.city,
required this.district,
required this.country,
required this.paymentType,
required this.memberCode,
required this.paymentNote,
required this.paymentAddress,
required this.erpOrderIds,
required this.erpStatus,
required this.erpStatusMsg,
required this.idNumber,
required this.taxNumber,
required this.paymentIp,
required this.space1,
required this.space2,
required this.space3,
required this.space4,
required this.space5,
required this.space6,
required this.space7,
required this.posId,
required this.createdIn,
required this.updatedIn,
});
final String id;
final String paymentsendId;
final String collectionEmail;
final String collectionSms;
final String periodOrder;
final String email;
final String cardType1;
final String cardType2;
final String cardNameSurname;
final String comment;
final String datumNull;
final String customerId;
final String orderid;
final String installmentNumber;
final String plusInstallment;
final String status;
final String maskedCard;
final String totalWithCommison;
final String totalWithoutCommison;
final String currencyType;
final String transactionData;
final String transactionBank;
final String transactionType;
final String partial;
final String nameSurname;
final String phoneNumber;
final String postCode;
final String city;
final String district;
final String country;
final String paymentType;
final String memberCode;
final String paymentNote;
final String paymentAddress;
final String erpOrderIds;
final String erpStatus;
final String erpStatusMsg;
final String idNumber;
final String taxNumber;
final String paymentIp;
final String space1;
final String space2;
final String space3;
final String space4;
final String space5;
final String space6;
final String space7;
final String posId;
final DateTime createdIn;
final DateTime updatedIn;
factory PaymentData.fromMap(Map<String, dynamic> json) => PaymentData(
id: json["id"],
paymentsendId: json["paymentsend_id"],
collectionEmail: json["collection_email"],
collectionSms: json["collection_sms"],
periodOrder: json["period_order"],
email: json["email"],
cardType1: json["card_type1"],
cardType2: json["card_type2"],
cardNameSurname: json["card_name_surname"],
comment: json["comment"],
datumNull: json["NULL"],
customerId: json["customer_id"],
orderid: json["orderid"],
installmentNumber: json["installment_number"],
plusInstallment: json["plus_installment"],
status: json["status"],
maskedCard: json["masked_card"],
totalWithCommison: json["total_with_commison"],
totalWithoutCommison: json["total_without_commison"],
currencyType: json["currency_type"],
transactionData: json["transaction_data"],
transactionBank: json["transaction_bank"],
transactionType: json["transaction_type"],
partial: json["partial"],
nameSurname: json["name_surname"],
phoneNumber: json["phone_number"],
postCode: json["post_code"],
city: json["city"],
district: json["district"],
country: json["country"],
paymentType: json["payment_type"],
memberCode: json["member_code"],
paymentNote: json["payment_note"],
paymentAddress: json["payment_address"],
erpOrderIds: json["erp_order_ids"],
erpStatus: json["erp_status"],
erpStatusMsg: json["erp_status_msg"],
idNumber: json["id_number"],
taxNumber: json["tax_number"],
paymentIp: json["payment_ip"],
space1: json["space1"],
space2: json["space2"],
space3: json["space3"],
space4: json["space4"],
space5: json["space5"],
space6: json["space6"],
space7: json["space7"],
posId: json["pos_id"],
createdIn: DateTime.parse(json["created_in"]),
updatedIn: DateTime.parse(json["updated_in"]),
);
factory PaymentData.fromJson(Map<String, dynamic> json) {
return PaymentData(
id: json["id"],
paymentsendId: json["paymentsend_id"],
collectionEmail: json["collection_email"],
collectionSms: json["collection_sms"],
periodOrder: json["period_order"],
email: json["email"],
cardType1: json["card_type1"],
cardType2: json["card_type2"],
cardNameSurname: json["card_name_surname"],
comment: json["comment"],
datumNull: json["NULL"],
customerId: json["customer_id"],
orderid: json["orderid"],
installmentNumber: json["installment_number"],
plusInstallment: json["plus_installment"],
status: json["status"],
maskedCard: json["masked_card"],
totalWithCommison: json["total_with_commison"],
totalWithoutCommison: json["total_without_commison"],
currencyType: json["currency_type"],
transactionData: json["transaction_data"],
transactionBank: json["transaction_bank"],
transactionType: json["transaction_type"],
partial: json["partial"],
nameSurname: json["name_surname"],
phoneNumber: json["phone_number"],
postCode: json["post_code"],
city: json["city"],
district: json["district"],
country: json["country"],
paymentType: json["payment_type"],
memberCode: json["member_code"],
paymentNote: json["payment_note"],
paymentAddress: json["payment_address"],
erpOrderIds: json["erp_order_ids"],
erpStatus: json["erp_status"],
erpStatusMsg: json["erp_status_msg"],
idNumber: json["id_number"],
taxNumber: json["tax_number"],
paymentIp: json["payment_ip"],
space1: json["space1"],
space2: json["space2"],
space3: json["space3"],
space4: json["space4"],
space5: json["space5"],
space6: json["space6"],
space7: json["space7"],
posId: json["pos_id"],
createdIn: DateTime.parse(json["created_in"]),
updatedIn: DateTime.parse(json["updated_in"]));
}
Map<String, dynamic> toMap() => {
"id": id,
"paymentsend_id": paymentsendId,
"collection_email": collectionEmail,
"collection_sms": collectionSms,
"period_order": periodOrder,
"email": email,
"card_type1": cardType1,
"card_type2": cardType2,
"card_name_surname": cardNameSurname,
"comment": comment,
"NULL": datumNull,
"customer_id": customerId,
"orderid": orderid,
"installment_number": installmentNumber,
"plus_installment": plusInstallment,
"status": status,
"masked_card": maskedCard,
"total_with_commison": totalWithCommison,
"total_without_commison": totalWithoutCommison,
"currency_type": currencyType,
"transaction_data": transactionData,
"transaction_bank": transactionBank,
"transaction_type": transactionType,
"partial": partial,
"name_surname": nameSurname,
"phone_number": phoneNumber,
"post_code": postCode,
"city": city,
"district": district,
"country": country,
"payment_type": paymentType,
"member_code": memberCode,
"payment_note": paymentNote,
"payment_address": paymentAddress,
"erp_order_ids": erpOrderIds,
"erp_status": erpStatus,
"erp_status_msg": erpStatusMsg,
"id_number": idNumber,
"tax_number": taxNumber,
"payment_ip": paymentIp,
"space1": space1,
"space2": space2,
"space3": space3,
"space4": space4,
"space5": space5,
"space6": space6,
"space7": space7,
"pos_id": posId,
"created_in": createdIn.toIso8601String(),
"updated_in": updatedIn.toIso8601String(),
};
}
When I print on Payment_model.dart, I get the following result.
[{id: 1, paymentsend_id: , collection_email: 0, collection_sms: 0, period_order: 0, email: [email protected], card_type1: cardfinans, card_type2: troy, card_name_surname: asktest, comment: , NULL: , customer_id: 123, orderid: 123123321, installment_number: 3, plus_installment: , status: 3D-POST, masked_card: 111111** **** 1111, total_with_commison: 1000.0000, total_without_commison: 1000.0000, currency_type: null, transaction_data: , transaction_bank: ing, transaction_type: 2, partial: , name_surname: asktest, phone_number: 1233123123, post_code: , city: Gaziantep, district: sahinbey, country: TR, payment_type: 3d, member_code: , payment_note: , payment_address: , erp_order_ids: , erp_status: , erp_status_msg: , id_number: , tax_number: , payment_ip: 111.111.11.11, space1: 12/2024, space2: , space3: , space4: log, space5: null, space6: null, space7: null, pos_id: null, created_in: 2021-11-26 12:54:07, updated_in: 0000-00-00 00:00:00}..]
CodePudding user response:
Your JSON has some NULL values. So it's not a good idea to make all your fields nonnullable. If you're sure about 'this field of the JSON will never have null value' then make it non-nullable. Otherwise, change them like this:
final String? comment;
...
remove required
this.comment
...
CodePudding user response:
You could change the fromMap
function in PaymentModel.dart
file like this to avoid access key with no value
factory PaymentData.fromMap(Map<String, dynamic> json) => PaymentData(
id: json["id"] == null ? "" : json["id"],
paymentsendId: json["paymentsend_id"] == null ? "" : json["paymentsend_id"],
collectionEmail: json["collection_email"] == null ? "" : json["collection_email"],
collectionSms: json["collection_sms"] == null ? "" : json["collection_sms"],
periodOrder: json["period_order"] == null ? "" : json["period_order"],
email: json["email"] == null ? "" : json["email"],
cardType1: json["card_type1"] == null ? "" : json["card_type1"],
cardType2: json["card_type2"] == null ? "" : json["card_type2"],
cardNameSurname: json["card_name_surname"] == null ? "" : json["card_name_surname"],
comment: json["comment"] == null ? "" : json["comment"],
datumNull: json["NULL"] == null ? "" : json["NULL"],
customerId: json["customer_id"] == null ? "" : json["customer_id"],
orderid: json["orderid"] == null ? "" json["orderid"]: ,
installmentNumber: json["installment_number"] == null ? "" : json["installment_number"],
plusInstallment: json["plus_installment"] == null ? "" : json["plus_installment"],
status: json["status"] == null ? "" : json["status"],
maskedCard: json["masked_card"] == null ? "" : json["masked_card"],
totalWithCommison: json["total_with_commison"] == null ? "" : json["total_with_commison"],
totalWithoutCommison: json["total_without_commison"] == null ? "" : json["total_without_commison"],
currencyType: json["currency_type"] == null ? "" : json["currency_type"],
transactionData: json["transaction_data"] == null ? "" : json["transaction_data"],
transactionBank: json["transaction_bank"] == null ? "" : json["transaction_bank"],
transactionType: json["transaction_type"] == null ? "" : json["transaction_type"],
partial: json["partial"] == null ? "" : json["partial"],
nameSurname: json["name_surname"] == null ? "" : json["name_surname"],
phoneNumber: json["phone_number"] == null ? "" : json["phone_number"],
postCode: json["post_code"] == null ? "" : json["post_code"],
city: json["city"] == null ? "" : json["city"],
district: json["district"] == null ? "" : json["district"],
country: json["country"] == null ? "" : json["country"],
paymentType: json["payment_type"] == null ? "" : json["payment_type"],
memberCode: json["member_code"] == null ? "" : json["member_code"],
paymentNote: json["payment_note"] == null ? "" : json["payment_note"],
paymentAddress: json["payment_address"] == null ? "" : json["payment_address"],
erpOrderIds: json["erp_order_ids"] == null ? "" : json["erp_order_ids"],
erpStatus: json["erp_status"] == null ? "" : json["erp_status"],
erpStatusMsg: json["erp_status_msg"] == null ? "" : json["erp_status_msg"],
idNumber: json["id_number"] == null ? "" : json["id_number"],
taxNumber: json["tax_number"] == null ? "" : json["tax_number"],
paymentIp: json["payment_ip"] == null ? "" : json["payment_ip"],
space1: json["space1"] == null ? "" : json["space1"],
space2: json["space2"] == null ? "" : json["space2"],
space3: json["space3"] == null ? "" : json["space3"],
space4: json["space4"] == null ? "" : json["space4"],
space5: json["space5"] == null ? "" : json["space5"],
space6: json["space6"] == null ? "" : json["space6"],
space7: json["space7"] == null ? "" : json["space7"],
posId: json["pos_id"] == null ? "" : json["pos_id"],
createdIn: DateTime.parse(json["created_in"]),
updatedIn: DateTime.parse(json["updated_in"]),
);
And do the same with fromJson
function