gpt4 book ai didi

flutter - 查询Dart中的对象列表?

转载 作者:行者123 更新时间:2023-12-03 04:56:21 26 4
gpt4 key购买 nike

我需要获取特定客户的所有详细信息:
我有一份 list ,列出所有客户的详细信息:

List<List<ClienteDetails>> _listaClientDetails = List<List<ClienteDetails>> ();

我想获取特定客户的详细信息,而我正在尝试通过使用以下代码来做到这一点:
var result = _listaProductosFactura.where((detailsList) => detailsList.where((cliente) => cliente.id == id)).toList();

但是这段代码是错误的。 “闭包的上下文要求,返回类型'Iterable'不是'bool'。”

有人知道什么是正确的代码吗?

最佳答案

您的代码的问题在于,这部分代码不会返回 bool(boolean) 值:

detailsList.where(((cliente)=> cliente.id == id)

它从内部列表中返回一个不是 bool(boolean) 值的iterable。
因此,第一个“where”方法无法返回可迭代对象,因为它没有 bool(boolean) 结果

var result = _listaProductosFactura.where((detailsList) => //you must have a boolean result here not an iterable 
//but detailsList.where((cliente) => cliente.id == id) gives you an iterable

您可以执行以下操作以获得所需的结果:
List<List<ClienteDetails>> _listaClientDetails = List<List<ClienteDetails>> ();
var result;

for(List<ClienteDetails> clienteDetails in _listaClientDetails){
result = clienteDetails.where((cliente)=>cliente.id == id).toList();
//goes through the inner list and get the element where cliente.id == id

}

关于flutter - 查询Dart中的对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60571840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com