This tutorial covers how to get the total value of items at a specific key of a listmap.
Prerequisites:
A listmap with logic already set up. This tutorial assumes you already have a listmap and want to calculate the total. If you do not know how to set up a listmap then you should check out my tutorial on how to create your own record keeping app.
Add a linear
Add a textview to the linear. This will display our total.
Go to the events tab
Add a more block as shown below
Name your block and then click add
Open the more block
In your more block, create 2 number variables "n" and "total".
Click 'add variable' and name your variables 'total' and 'n'
NB- If you already have a variable named n or total, then give it another name. In my case I called my total variable, "total1"
Then place your blocks to get the total like this:
In my example, I get the total and display it as a string in the decimal format 0.00
I also use a 'shared preferences' component to save this total on the device storage. You can leave this out if you do not need it.
Also remember that list keys are case sensitive... so calling the wrong key might crash your app.
The next step is displaying the total
You might want to show your total when a button is clicked or when your app starts. In the following example I placed my logic on start. That works if I already have items on my list when the app starts. This calls that moreblock logic when the app starts. If you want to show the total when a button is clicked, place your logic onButtonClick.
Get the total number of items in a list
Use the length of list block to get how many items a specific list is holding
Comments