I am developing a restaurant delivery app and I am having problems when adding the same product on the list but with different values, it overwrite the value from the old ones.
I have a Store called BaseStore that has a "Bag" object as an Observable.
abstract class _BaseStore with Store {
@observable
Bag bag = Bag(
products: ObservableList<Product>()
);
@action
addProduct(Product product){
bag,products.add(products);
}
}
I am registering BaseStore as Singleton using get_it package: https://pub.dev/packages/get_it
GetIt.I.registerSingleton<BaseStore>(BaseStore());
The Bag class has an ObservableList of Product object.
class Bag {
ObservableList<Product> products;
}
The Product class has more variables but I'll only show this two for the example:
class Product {
String name;
List<Option> options;
}
class Option {
String optionSelected;
List<ItemOption>
}
class ItemOption {
String name;
}
I'm trying to add two products, both has the same values, the only difference is the String "optionSelected" from the "Option" class that on the example below I created two Products and they have diferent "optionSelected" values (Diet and other Normal).
Product product1 = Product(
name: "Coke",
options: [
Option(optionSelected: "Diet")
]
);
Product product2 = Product(
name: "Coke",
options: [
Option(optionSelected: "Normal")
]
);
I call the action from the singleton and print the values.
GetIt.instance<BaseStore>().addProduct(product1);
GetIt.instance<BaseStore>().addProduct(product2);
bag.products.forEach((product){
product.options.forEach((option)=> print(option.optionSelected));
});
it prints:
Normal
Normal
When I add a second product with all same values, except one value from inner list (Option's optionSelected) it changes the all products values from the list according to the last added.
Example when adding different product, some different values, the issue doesn't happen: https://i.imgur.com/ULzdPRM.mp4
When adding the same product but with different optionSelected and quantity: https://i.imgur.com/vlgKkq5.mp4