我在 Dart 中有两个列表,如下所示,
final List availableIssueComponents = [
{'id': 1, 'componentName': 'Cash Acceptor'},
{'id': 2, 'componentName': 'Printer'},
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 4, 'componentName': 'Key Board'},
{'id': 5, 'componentName': 'Touch Screen'},
{'id': 6, 'componentName': 'Computer'},
{'id': 7, 'componentName': 'Application'},
{'id': 8, 'componentName': 'Network'},
{'id': 9, 'componentName': 'Power'},
{'id': 10, 'componentName': 'Camera'},
{'id': 11, 'componentName': 'Safe'},
{'id': 13, 'componentName': 'Screen'},
{'id': 14, 'componentName': 'Battery'},
{'id': 15, 'componentName': 'Ports'},
{'id': 16, 'componentName': 'Application'},
{'id': 17, 'componentName': 'Safe'},
{'id': 18, 'componentName': 'Camera'},
{'id': 19, 'componentName': 'Power'},
{'id': 20, 'componentName': 'Key Board'},
{'id': 21, 'componentName': 'PIN Pad'},
{'id': 22, 'componentName': 'Printer'},
{'id': 23, 'componentName': 'Computer'},
{'id': 24, 'componentName': 'Touch Screen'},
{'id': 25, 'componentName': 'Application'},
{'id': 26, 'componentName': 'Network'}
];
final List selectedIssueComponents = [
{'id': 3, 'componentName': 'PIN Pad'},
{'id': 6, 'componentName': 'Computer'},
{'id': 19, 'componentName': 'Power'},
];
从以上两个列表中,我试图从availableIssueComponents
排除selectedIssueComponents
.
例如:由于 id 为3
,的组件在两个列表6
中19
都很常见,所以我想要第三个列表,其中包含除 id 为3
, 6
,的组件之外的所有组件19
。
第三个列表应如下所示,
final List availableIssueComponents = [
{'id': 1, 'componentName': 'Cash Acceptor'},
{'id': 2, 'componentName': 'Printer'},
{'id': 4, 'componentName': 'Key Board'},
{'id': 5, 'componentName': 'Touch Screen'},
{'id': 7, 'componentName': 'Application'},
{'id': 8, 'componentName': 'Network'},
{'id': 9, 'componentName': 'Power'},
{'id': 10, 'componentName': 'Camera'},
{'id': 11, 'componentName': 'Safe'},
{'id': 13, 'componentName': 'Screen'},
{'id': 14, 'componentName': 'Battery'},
{'id': 15, 'componentName': 'Ports'},
{'id': 16, 'componentName': 'Application'},
{'id': 17, 'componentName': 'Safe'},
{'id': 18, 'componentName': 'Camera'},
{'id': 20, 'componentName': 'Key Board'},
{'id': 21, 'componentName': 'PIN Pad'},
{'id': 22, 'componentName': 'Printer'},
{'id': 23, 'componentName': 'Computer'},
{'id': 24, 'componentName': 'Touch Screen'},
{'id': 25, 'componentName': 'Application'},
{'id': 26, 'componentName': 'Network'}
];
我尝试使用 Sets 来做到这一点,以下是我的方法,
Set availableComponentsSet = Set.from(availableIssueComponents);
Set issueComponentsSet = Set.from(selectedIssueComponents);
Set resultComponents = availableComponentsSet.difference(issueComponentsSet);
但是当登录到控制台时,它resultComponents
包含所有组件。这不是我想要的。我也尝试了嵌套的 for 循环,但它也不起作用。