Reputation: 107
I am creating a POS where i want to show the items purchased as a text and i intent to store those as a text too. I am separating each item by '\n' character.
Now the problem is that i dont know how to check for text which specifically matches a certain product. I have tried using string.contains method but it is not what i desire.
For example: I have products names like:
- Chicken
- Chicken Cheese
Now these are two different products but every chicken cheese is going to contain chicken too in it.
So, How can i replace the product
Products are in a list which are further part of a map. However, for this question; you can assume that all the items are stored as a list, Example below:
['chicken', 120],
['chicken Cheeze', 150],
['chicken Cheeze Pizza', 180],
.......................
Than, down in the code i am saving those products as a string (when tapped upon.).
// Gesture Detector is inside a Galleryview which is further part of a row and expanded widgets,
GestureDetector(
onTap: () {
//bill items is a string Storing all products.
billItems += (item[0] +
' : ' +
item[1].toString() + '\n');
},
P.s: All i want to do is to do something like: "chicken * 3" if chickens are orders 3 times instead of printing it 3 times. Thanks
Upvotes: 0
Views: 1159
Reputation: 4666
So, I have came up with a solution, though I don't know how optimal it is or fast.
I first create a product model to serialize it :
class Product {
String name;
double price;
int count;
Product({required this.name, required this.price,this.count=0});
factory Product.fromDynamic(List product) =>
Product(name: product[0], price: product[1]);
Map<String, dynamic> toJson() {
return {"name": name, "price": price,"count":count,};
}
}
So assuming our products are in this form :
final originalProducts = [
['chicken', 120],
['chicken', 135],
['chicken', 140],
['chicken Cheeze', 150],
['chicken Cheeze Pizza', 180],
];
I created a List<Product>
:
List<Product> modeledProducts =
List.generate(originalProducts.length, (i) => Product.fromDynamic(originalProducts[i]));
Next I create a temporary List<String>
where I will save any duplicate products found (their names), and I create the final List<Products>
which will have unique products with their prices summed up :
List<String> duplicates = [];
List<Product> finalProducts = [];
Then we iterate through the modeledProducts
to find the duplicates and do the necessary operations :
for (final product in modeledProducts) {
if (!duplicates.contains(product.name)) {
duplicates.add(product.name);
finalProducts.add(product);
} else {
finalProducts.firstWhere((p) => p.name == product.name).price +=
product.price;
finalProducts.firstWhere((p) => p.name == product.name).count++;
}
}
And now you are ready to use your data :
for (final fProduct in finalProducts) {
print(fProduct.toJson());
}
The above will print :
{name: chicken, price: 395, count: 3}
{name: chicken Cheeze, price: 150, count: 1}
{name: chicken Cheeze Pizza, price: 180, count: 1}
Upvotes: 1