Shopping Card Manage Dart or Flutter

Shopping Card Manage Dart or Flutter :

This flutter tutorial post is shopping cart manage.

  • This program 4 items shopping Flutter Tutorial, Flutter 2 items, Android, Java.
  • Total Amount is 1100.0
  • Total Shopping cart items 4

Functionality :

  • Shopping Add items multiple (if item is already add then update cart otherwise new items add.)
  • Total Amount
  • Remove shopping cart item
  • Total amount
  • Shopping cart manage Dart Map using.
    1) Map dart new item add
    2) Map dart update item
    3) Map dart remove item
    4) Map dart check contain (containsKey) value
Shopping Card Manage Dart or Flutter

Shopping Cart Manage Dart or Flutter :

import 'package:uuid/uuid.dart';

main() {
  Carts carts = new Carts();
  carts.items.values.toList();

  carts.addItem('1', 'Flutter Tutorial', 100);
  carts.addItem('2', 'Flutter', 100);
  carts.addItem('3', 'Android', 100);
  carts.addItem('4', 'Java', 700);
  carts.addItem('2', 'Flutter', 100);

  print('Shopping items ' + carts.itemCount.toString());
  print('Total Amount ' + carts.totalAmount.toString());
}

class Cart {
  final String id;
  final String title;
  final int quantity;
  final double price;

  Cart({
    this.id,
    this.title,
    this.price,
    this.quantity,
  });
}

class Carts {
  final uuid = Uuid();
  Map<String, Cart> _items = {};

  Map<String, Cart> get items {
    return {..._items};
  }

  int get itemCount {
    return _items.length;
  }

  double get totalAmount {
    var total = 0.0;
    _items.forEach((key, cart) => total += cart.price * cart.quantity);

    return total;
  }

  void addItem(String productId, String title, double price) {
    if (_items.containsKey(productId)) {
      _items.update(
        productId,
        (old) => Cart(
          id: old.id,
          title: old.title,
          price: old.price,
          quantity: old.quantity + 1,
        ),
      );
    } else {
      _items.putIfAbsent(
        productId,
        () => Cart(
          id: uuid.v1(),
          title: title,
          price: price,
          quantity: 1,
        ),
      );
    }
  }

  void removeItem(String productId) {
    _items.remove(productId);
  }

  void removeSingleItem(String productId) {
    if (!_items.containsKey(productId)) return;

    if (_items[productId].quantity > 1) {
      _items.update(
          productId,
          (old) => Cart(
                id: old.id,
                title: old.title,
                price: old.price,
                quantity: old.quantity - 1,
              ));
    } else {
      _items.remove(productId);
    }
  }

  void clear() {
    _items = {};
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in