Local Cart Storage
core/services/local_storage.dart · getLocalCartItems / save / clear
Overview
Per-user SharedPreferences slice for the local cart. Keyed by customer id so that multiple accounts on the same device keep separate carts.
User Flow
- getLocalCartItems(userId) reads the JSON string under the user-scoped key and runs LocalCartItem.decodeList.
- saveLocalCartItems(userId, list) serializes via encodeList and writes the JSON string.
- clearLocalCartItems(userId) removes the key entirely — used by CartController.clearCart and on logout.
Cases & Edge Cases
- decodeList swallows JSON errors → corrupt storage resets to []. Acceptable because a busted local cart is a recoverable inconvenience.
- Keys are not migrated when a user changes id (server merges accounts) — the old cart becomes orphaned in storage.
- No size cap; a malicious or buggy add loop could grow the JSON beyond what SharedPreferences happily handles on Android.
Code References
- lib/src/core/services/local_storage.dart:53 // getLocalCartItems
- lib/src/core/services/local_storage.dart:61 // saveLocalCartItems
- lib/src/core/services/local_storage.dart:70 // clearLocalCartItems
API Calls
Notes
Consider moving to flutter_secure_storage when persisting any cart that includes a backend-cart id post-checkout-start. Local storage contracts (SharedPreferences): • getLocalCartItems(userId) — read 'local_cart_<userId>' → List<LocalCartItem> (decodeList tolerates corrupt JSON → [] ) — screens: cart_screen.dart, product_details_screen.dart (badge count) • saveLocalCartItems(userId, items) — write encodeList(items) JSON string — screens: cart_screen.dart, product_details_screen.dart (after addToCart / updateQuantity / removeItem) • clearLocalCartItems(userId) — remove key — screens: cart_screen.dart (clearCart) + post-completeCart in checkout_screen.dart