Home > Blockchain >  API How to filter item in Flutter?
API How to filter item in Flutter?

Time:01-08

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'

CodePudding user response:

.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 think 2 is better because we are only doing one filter operation to the list.

Also, .where will return Iterable which is the superclass of List. As your function is returning a List, we have to use .toList() to convert the Iterable to List.

  1. Reassign the original list

    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);
    
    // Reassign the result of .where to modifiedUserData
    var modifiedUserData = userData.loan?.where((element) => element.status == "Closed");
    
    return modifiedUserList.toList();
    
  2. Return the modified list

    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);
    
    // Return the result of .where directly
    return userData.loan?.where((element) => element.status == "Closed").toList();
    

CodePudding user response:

  • Hope it helps you
void test() {
  var json =
      '{"loan":[{"id":"1", "icbsid": "55","loanId": "null", "status": "Closed"},{"id":"2", "icbsid": "55","loanId": "null", "status": "None"},{"id":"3", "icbsid": "25","loanId": "sss", "status": "None"} ]}';
  final jsonData = jsonDecode(json);
  var map = Map<String, dynamic>.from(jsonData);
  var data = UserModel.fromJson(map);
  data.loan?.removeWhere((model) => model.status != 'Closed');
  data.loan?.forEach((model) {
    pr('id::${model.id}  status::${model.status}');
  });
}

class UserModel {
  List<Loan>? loan;
  UserModel(this.loan);
  factory UserModel.fromJson(Map<String, dynamic> json) => UserModel(
      (json['loan'] as List<dynamic>?)?.map((e) => Loan.fromJson(e)).toList());
}

class Loan {
  String? id;
  String? status;
  Loan(this.id, this.status);
  factory Loan.fromJson(Map<String, dynamic> json) =>
      Loan(json['id'], json['status']);
}
  • Related