1

I have 2 arrays cart and cartCopy, inside that array I have an object. Data is inserted in cart and cartCopy. When updating the variant or extras in cart I first create a separate list for them variantList=[] and extrasList=[], now when updating variantList/extrasList the values in cart and cartCopy also changes even though I did not update them.

{"product_name:"",variants:[],extras:[]}

Provider class changing variant for reference and Setting Product to Edit. Whenever I update the variant, cart and cartCopy variant also changes. How to I separate the values in cart?

class ProductDetailsProvider extends ChangeNotifier {
  List<VariantDetails>? variants = [];
  List<VariantDetails>? selectedVariants = [];
  List<AddOnDetails>? selectedExtras = [];

    addVariant(VariantDetails? item) {
        var exist =
            variants?.indexWhere((element) => element.typeID == item?.typeID);
        if (exist != -1) {
          variants?[exist!] = item!;
        } else {
          variants?.insert(0, item!);
        }
      }

  setProduct(CartData item) async {
    variants = item.variant;
    selectedExtras = item.extras;
    selectedVariants = item.variant;
    notifyListeners();
  }
     }

Add to Cart Class

class CartProvider extends ChangeNotifier {
  List<CartData>? cart = [];
  List<CartData>? cartCopy = [];

 addToCart(context, CartData cartItem, isUpdate) async {
      cart?[exist!] = cartItem;
      cartCopy?[exist!] = cartItem;
   
    }}
Ken White
  • 123,280
  • 14
  • 225
  • 444
VDTe
  • 438
  • 1
  • 4
  • 17
  • 1
    Where do you copy cart to cartCopy? I had a similiar issue with a client class so I copy it with this: `activeClient = Client.decode(Client.encode(newClient));`. Maybe try something like that to ensure that your not giving a reference but copying the values. – Alejandro Cumpa Feb 17 '23 at 15:27
  • Sorry updated it. I insert data in cart and cartCopy, then on ProductDetailsProvider class I set the values for variants array, when updating that array the variants in cart and cartCopy also changes. – VDTe Feb 18 '23 at 00:59
  • Is client the name of the model? I don't seem to have decode in mine. – VDTe Feb 18 '23 at 01:07

1 Answers1

1

You can use the following methods to encode and decode the list of CartData

static String encode(List<CartData> cart) => json.encode(cart
     .map<Map<String, dynamic>>((value) => CartData.toMap(value))
     .toList(),
);

static List<CartData> decode(String cart) {
  return cart.isNotEmpty
  ? (json.decode(cart) as List<dynamic>)
          .map<CartData>((item) => CartData.fromJSON(item))
          .toList()
 : [];

And in CartData class you should have something like this for the object

CartData.fromJSON(Map<String, dynamic> jsonMap)
      : id = jsonMap['id'],            
        name = jsonMap['name'],
        price = jsonMap['price'],
        tax = jsonMap['tax'];


static Map<String, dynamic> toMap(CartData cartItem) => {
        'id': cartItem.id,
        'name': cartItem.name,
        'price': cartItem.price,
        'tax': cartItem.tax
};

Those methods would allow you to do this:

cartCopy = CartData.decode(CartData.encode(cart));
Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45