0

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

replace the text according to given info above in the question

6
  • am not following you , you have to make item description column and beside it a qty box . now if the chicken is matching with different items that fine , you should filter based on first letter not the word it self , so if you start with C all the C items will appear and so on till you get to right the chiken word , then it will filter cheese once you follow the chiken with c . is that what you want ? Commented Dec 17, 2021 at 21:44
  • i can share a page where i did that already if you like to see how i did it Commented Dec 17, 2021 at 21:45
  • you can use bar code scanner too to capture items Commented Dec 17, 2021 at 21:45
  • Can you please share some code ? Specifically where you populate the items purchased. Commented Dec 17, 2021 at 22:06
  • @esentis I have updated y question with code. Hopefully it will help to understand the problem. Thanks for your time. in case of any confusion please dont hesitate to ask: Commented Dec 18, 2021 at 15:15

1 Answer 1

1

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}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Esentis, Thanks for your answer. I think it is a complicated and computationally / resource expensive way to overcome the problem.
It took me a while to figure it out. The best way of completing the task is by using "MAP<>". We need to store the name as key and price as value. it will assign a new value upon finding the key. Thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.