I want to break the function after the if statement, but I unable to do so.
Below is my code snippet.
void addOrderToCart(Product product, int quantity, String color, String size) { _lastOrder = Order(product, quantity, _orderId++, color, size); _orders.forEach((element) { if(element.product.id == _lastOrder.product.id){ element.colors.add(color); element.sizes.add(size); element.quantity = element.quantity + quantity; notifyListeners(); return; } }); _orders.add(_lastOrder); notifyListeners(); }
Thanks.
I think you should return bool or any other instead of void and use for instead of forEach.
Here's the solution you looking for.
bool addOrderToCart(Product product, int quantity, String color, String size) { _lastOrder = Order(product, quantity, _orderId++, color, size); for(var element in _orders){ if (element.product.id == _lastOrder.product.id) { element.colors.add(color); element.sizes.add(size); element.quantity = element.quantity + quantity; notifyListeners(); return true; } } _orders.add(_lastOrder); notifyListeners(); return true; }
Hope this helps.
Good day.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With