1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| class MatManager {
| List<Map<String, dynamic>> items = [];
|
| MatManager(this.items);
|
| void matManager({int? index, Map<String, dynamic>? newItem}) {
| if (index != null && newItem != null) {
| // 更新指定索引的对象
| items[index] = newItem;
| } else if (newItem != null) {
|
| // 添加新项目
| items.add(newItem);
| } else {
| print('请提供有效的索引或项目');
| }
| }
|
| void addOrUpdateItem({required var newItem, required var fieldsToMatch}) {
| for (var item in items) {
| bool isMatch = fieldsToMatch.every((field) => item[field] == newItem[field]);
| if (isMatch) {
| item['quantity'] = (item['quantity'] ?? 0) + (newItem['quantity'] ?? 0);
| return;
| }
| }
| items.add(newItem);
| }
| }
|
|