What is the correct process of keep consistency of data at add to cart functionality when using flutter_bloc?

104

You can store the items of the cart in some persistent database or storage, so when the user opens the app again the items would be pre-loaded in the cart bloc. For this purpose, you can use the Hive, Shared Preference or sqflite. Use which one works best for you.

The proper way would be to store items in storage right after the user adds them, and clear the storage right after the user clears its cart, so he does not see any previous item upon opening the app next time.

Maintaining the cart for each user in persistent storage should not be a problem as it is stored in the mobile's memory. But if you are asking for multiple users using the same device having different cart items then you can assign a unique id or value to identify which user has logged in.

Share:
104
tharaka_dil95
Author by

tharaka_dil95

Undergraduate at sabaragamuwa university of Sri Lanka.

Updated on December 26, 2022

Comments

  • tharaka_dil95
    tharaka_dil95 over 1 year

    I am implementing add to cart functionality in a flutter app with using BLoC pattern as state management, only a user who is logged can add products to the cart and as the first step, I want to store that cart data locally before trigger a POST request to the database. Also, there should be a facility to user to remove items from cart if it's unnecessary. Further, I want to keep the cart in the same state after a user re-open the application who logged in before. Is it possible with flutter_secure_storage and how I do in a proper way? And, how I maintain a cart to each user?