How to build a list using stored variables

I need to build some lists using stored variables so the list contents are remembered between uses. I can get this to work using app variables but the lists I am creating are blank the next time the app is run due to the fact I have to declare the variables as empty lists. I also need to ensure that one particular item is always first so I cant just take an app variable and say Stored variable =app variable if a new item is added. I have tried the following blocks.


If I leave the third list block out then the app works fine while it is open. The listviewer shows the detail entered as expected until the app is closed. When I add the third list block the app hangs when the item is submitted. I think I am close but there is clearly something I am missing. Any ideas on this. I have looked through the previous posts but perhaps my search criteria are wrong.

1 Like

Initialize device variables list1
if list1==null
list1=empty list

I think you have an extra block in there. Try removing the circled block

2 Likes

Yes @catsarisky is correct as always. You are trying to reference list inside list which will result in null for your situation.

1 Like

I tried removing that block but it didnt change the behaviour.

. I think this is because the stored variable is never declared as a list. I know thunkable variables can change but lists seem to need to be declared. tony-ycy might be on to something. I’ll test that next. @catsarisky thanks for taking the trouble to look.

Oh no, Muneer. Sometimes I’m wrong. :wink:

3 Likes

Worth trying. So you’d initialize it to an empty list. I’m wondering if your problem is that TempNickNameList isn’t getting set by the first list block, too. Text_Nickname definitely has some text in it? (Edit: Oh, I see that you check that in the if statement enclosing this section. Right.)

I find that using some labels to look at what values are being passed around is really helpful. (For lists, I’ll use “create text from list” to make it so I can shove the list onto a label temporarily.)

2 Likes

Thunkable has a logic issue with variables to store lists.

A variable that is “null” cannot be used to store a list. To overcome this issue first check if the variable is null and if so set it to empty list first then you can add items to it.

2 Likes

I think I have it sorted. Not fully tested but initially it looks like this works.

image

I don’t like using null with thunkable because sometimes it returns a true null and other times it’s the word null which doesn’t evaluate to a logic test for null.
But thanks to @tony.ycy.program , @catsarisky and @muneer for your thoughts and ideas.
Now I just have to get the list viewer working :slight_smile:

1 Like

Sorry guys. I thought I had cracked it but I was wrong. If I have these blocks at the start of the app.

, everything stops and the app doesnt do anything. I can get to an edit page where I am supposed to be able to update the list but nothing happens when I click the submit button. I hate lists :slight_smile: Is there something obvious here that I am missing?

1 Like

Empty means ""
But Device variables default value is null
empty string != null

Thanks Tony, Looks like you are right. I removed the Empty check and it is working as expected. The logic I had still seems ok to me as I thought the OR would trigger the IF in the event that either condition was true. The second part would be true when first run so set the empty list. When run a second time the list would not be empty and the variable would not be null so the IF would be skipped. Where is that wrong? I cant argue with the results though. Thanks.

1 Like

I think the reason your app crashed rather than returning false is because you asked it to check if a stored variable’s value is empty (""). But a stored variable has NO value to start with. So the check itself fails. The “if” block never gets to evaluate the “or” section because it’s already crashing.

1 Like

Thanks @tatiang. My reasoning was empty("") doesn’t equal no value so the IF condition should work. Logically that is. But I cant argue with the results I am seeing and they do validate what you and @tony.ycy.program @muneer and others have been saying. Thank you for clarifying.

1 Like

I totally get what you’re saying and I think it’s reasonable to think of it that way. From a human perspective, we think “If A or B” means that you check A and B and if either are true, you return true.

But my guess is that your blocks actually get evaluated in a specific order:

  1. Does the stored variable have a value and is that value empty ("")?
    CRASH
  2. Does the stored variable have no value assigned (null)?
  3. Are either 1 or 2 true? If so, return true; if not, return false.

It’s a little like if I asked Thunkable to evaluate whether “1 ÷ 0 = 6 OR whether 1 x 0 = 6.” It wouldn’t return false because it wouldn’t know what to do with 1 ÷ 0 since it’s undefined.

Anyway, if you stick to checking stored variables for null values on startup, you should be fine! :wink:

3 Likes