简体   繁体   English

使用flutter_bloc时在添加到购物车功能时保持数据一致性的正确过程是什么?

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

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.我正在flutter应用程序中实现添加到购物车功能,并使用BLoC pattern作为 state 管理,只有登录的用户才能将产品添加到购物车,作为第一步,我想在触发 POST 请求之前在本地存储购物车数据到数据库。 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.此外,在用户重新打开之前登录的应用程序后,我想将购物车保留在同一个 state 中。 Is it possible with flutter_secure_storage and how I do in a proper way? flutter_secure_storage是否有可能以及我如何以正确的方式进行操作? And, how I maintain a cart to each user?而且,我如何为每个用户维护一个购物车?

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 .为此,您可以使用HiveShared Preferencesqflite 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.在持久存储中维护每个用户的购物车应该不是问题,因为它存储在移动设备的 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.但是,如果您要求多个用户使用具有不同购物车项目的同一设备,那么您可以分配一个唯一的 ID 或值来识别哪个用户已登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM