Response from API:
"loan": [
{
"id": "612",
"icbsid": "55",
"loanId": "null",
"loanAcctNo": "001-063-06881-1",
"productId": "4",
"productName": "Fixed Principal+Int(Adv Pym)",
"approvedDate": "2017-11-13",
"loanAmount": "7359.97",
"loanBalance": "0.0",
"monthsToPay": "36",
"interestRate": "12.0",
"dueDate": "2020-12-13",
"status": "Closed",
"lastPayment": "2020-01-10"
},
{
"id": "4970",
"icbsid": "55",
"loanId": "16",
"loanAcctNo": "001-263-01625-4",
"productId": "6",
"productName": "Regular Long Term",
"approvedDate": "2022-01-27",
"loanAmount": "9934.21",
"loanBalance": "5384.21",
"monthsToPay": "60",
"interestRate": "0.0",
"dueDate": "2027-08-25",
"status": "Disbursed",
"lastPayment": "2022-12-29"
}
]
This is my code and it’s working fine, but I need to filter the status
` @override
Future<List?> fetchLoanList() async {
final response = await httpServices.getRequest('mobileAppGetIcbsid?icbsid=001-0000055');
final jsonData = json.decode(response.data);
var map = Map<String, dynamic>.from(jsonData);
var userData = UserModel.fromJson(map);
// userData.loan?.where((element) => element.status == "Closed"); <-- not working for me
return userData.loan;
}`
I tried to uncomment this code userData.loan?.where((element) => element.status == "Closed");
it is working fine for displaying the data but not filtering the status. I am expecting to display only the data where status == ‘Closed’
2
Answers
.where
will create a new modified list but not modify the original list. Either reassign the original list or return the modified list. In this case I think2
is better because we are only doing one filter operation to the list.Also,
.where
will returnIterable
which is the superclass ofList
. As your function is returning aList
, we have to use.toList()
to convert theIterable
toList
.Reassign the original list
Return the modified list