fix(shopping_list): genera ID univoci in ShoppingListProvider
add() usava millisecondsSinceEpoch come ID: chiamate ravvicinate nello stesso millisecondo producevano ID identici, causando toggle e remove incorretti su più elementi. Introdotto _idSeq come contatore progressivo, allineato all'approccio già usato in addAll(). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import '../../../core/constants/app_constants.dart';
|
|||||||
|
|
||||||
class ShoppingListProvider extends ChangeNotifier {
|
class ShoppingListProvider extends ChangeNotifier {
|
||||||
List<ShoppingItem> _items = [];
|
List<ShoppingItem> _items = [];
|
||||||
|
int _idSeq = 0;
|
||||||
|
|
||||||
List<ShoppingItem> get items => _items;
|
List<ShoppingItem> get items => _items;
|
||||||
List<ShoppingItem> get pendingItems =>
|
List<ShoppingItem> get pendingItems =>
|
||||||
@@ -39,7 +40,7 @@ class ShoppingListProvider extends ChangeNotifier {
|
|||||||
_items = [
|
_items = [
|
||||||
..._items,
|
..._items,
|
||||||
ShoppingItem(
|
ShoppingItem(
|
||||||
id: DateTime.now().millisecondsSinceEpoch.toString(),
|
id: '${DateTime.now().millisecondsSinceEpoch}_${_idSeq++}',
|
||||||
name: t,
|
name: t,
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
@@ -61,11 +62,10 @@ class ShoppingListProvider extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final toAdd = <ShoppingItem>[];
|
final toAdd = <ShoppingItem>[];
|
||||||
var i = 0;
|
|
||||||
for (final entry in counts.entries) {
|
for (final entry in counts.entries) {
|
||||||
if (existing.contains(entry.key)) continue;
|
if (existing.contains(entry.key)) continue;
|
||||||
toAdd.add(ShoppingItem(
|
toAdd.add(ShoppingItem(
|
||||||
id: '${DateTime.now().millisecondsSinceEpoch}_${i++}',
|
id: '${DateTime.now().millisecondsSinceEpoch}_${_idSeq++}',
|
||||||
name: canonical[entry.key]!,
|
name: canonical[entry.key]!,
|
||||||
quantity: entry.value,
|
quantity: entry.value,
|
||||||
));
|
));
|
||||||
|
|||||||
Reference in New Issue
Block a user