0

I have created Shopping cart for ecommerce app. I am able to add new item to cart with quantity 1, but other condition of If where item is already in cart, quantity is not increasing by + 1. I am not getting any error as well in console.

Also, How do i save this in local database. whenever i refresh app, cart items becomes 0.

class CartItemStored with ChangeNotifier {
     Map<String, Product> _newitemList= {};

     Map<String, Product> get newItemList{
       return{..._newitemList};
     }

     List<Product> _list =[];
     List<Product> get list{
       return[..._list];
     }

  int get itemListCount {
    return newItemList.length;
  }

     double get totalAmount {
       var total = 0.0;
       _newitemList.forEach((key, cartItem) {
         total += cartItem.productListPrice  * cartItem.quantity;
       });
       return total;
     }

  void addtoCart(
    int productId,
    String productName,
    int productListPrice,
    int productSalePrice,
    String productSku,
    int quantity,
  ) {
    if (_newitemList.containsKey(productId)){
      _newitemList.update(productId.toString(), (existingCartItem) => Product(
        productId: productId,
        productName: productName,
        productListPrice: productListPrice,
        productSalePrice: productSalePrice,
        productSku: productSku,
        quantity: quantity + 1,
      ));
    } else{
      _newitemList.putIfAbsent(productId.toString(), ()=> Product(
        productId: productId,
        productName: productName,
        productListPrice: productListPrice,
        productSalePrice: productSalePrice,
        productSku: productSku,
        quantity: 1,
      ));

    }notifyListeners();
    DBHelper.insert('cart', {
      'productId': productId,
      'productName': productName,
      'productListPrice': productListPrice,
      'productSalePrice': productSalePrice,
      'productSku': productSku,
      'quantity': quantity,
    });

  }

  Future<void> fetchAndSetCart() async {
    final dataList = await DBHelper.getCartData('cart');
    _newitemList = dataList.map((itemList) => Product(
      productId: itemList['productId'],
      productName: itemList['productName'],
      productSalePrice: itemList['productSalePrice'],
      productListPrice: itemList['productListPrice'],
      productSku: itemList['productSku'],
      quantity: itemList['quantity'],

    )) as Map<String, Product>;
    notifyListeners();
  }
}

class DBHelper {
  static Future<Database> database() async {
    final dbPath = await sql.getDatabasesPath();
    return sql.openDatabase(path.join(dbPath, 'cart.db'),
        onCreate: (db, version) {
      db.execute(
          'CREATE TABLE cart(productId TEXT, productName TEXT,productListPrice TEXT,productSalePrice TEXT,productSku TEXT,quantity TEXT)');
    }, version: 1);
  }

  static Future<void> insert(String table, Map<String, Object> data) async {
    final db = await DBHelper.database();
    db.insert(table, data, conflictAlgorithm: sql.ConflictAlgorithm.replace);
  }

  static Future<List<Map<String, dynamic>>> getCartData(String table) async {
    final db = await DBHelper.database();
    return db.query(table);
  }
}

1 Answer 1

-2

I think you should use Shared Preferences to save data. Here is the package :package

2
  • I am looking to store Cart data only in local storage so Sqflite is fine for that.. I am not sure what i am doing wrong as after app is refreshed, data doesn't show.. i need to fix that Commented Jun 7, 2020 at 8:39
  • I am sorry for that but I can't help you with that:Just I am using shared preferences to save string and int. It sounds stupid but maybe you can save the count of cart items with shared preferences as int and add a for loop to add items as much as saved in shared preferences, also you can save the items name with shared preferences as string.
    – Ege
    Commented Jun 7, 2020 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.