API如何在Flutter中过滤项目?

nvbavucw  于 2023-01-09  发布在  Flutter
关注(0)|答案(2)|浏览(117)

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"
    }
]

这是我的代码,运行良好,但我需要过滤status
'@覆盖Future〈List?〉fetchLoanList()异步{

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;

}我尝试取消注解此代码userData.loan?.where((element) => element.status == "Closed");`,它可以正常显示数据,但不能过滤状态。我希望仅显示status =="Closed"的数据

nbysray5

nbysray51#

.where将创建一个新的修改列表,但不修改原始列表。重新分配原始列表或返回修改后的列表。在这种情况下,我认为2更好,因为我们只对列表执行一次过滤操作。
同样,.where将返回Iterable,它是List的超类。由于您的函数返回List,因此我们必须使用.toList()Iterable转换为List
1.重新分配原始列表

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();

1.返回修改后的列表

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();
pdsfdshx

pdsfdshx2#

  • 希望对你有帮助
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']);
}

相关问题