Saving parameters with dictionary

Hi,
I'm new in App Inventor. I'm trying to build a simple App where I can store some parameters(3 pairs with dictionary) to a file with a button and read back with another button. But it always crashes without error message when changing to the next text box for value change. Just "undefined" will be shown.
image
Does anybody has an idea?
dict_test.aia (3.6 KB)

Don't use the LostFocus events, use a button click event to store all the new values in your dictionary

Taifun

I don't see where you are issuing the read for the file.

Also, assuming your file is being stored as JSON, you would need the Web1.JSONTextDecodeAsDictionaries block in all 4 places to get a dictionary back.

It is unwise to default on naming variables and procedures. Try global testDict to at least announce its expected data type to any one forced to read your code.

Hi Taifun, thanks for the reply. I'm using "lostFocus" because I want to write the value every time I'm switching to the next input field/text box. At the input filelds/text box are only "lost focus" and "got focus" available. In case of "button click" I need a button each text box additionally. This makes it very uncomfortable...
What is the reason about crashing while using "lost focus"?

You need only one button to click after all textboxes are filled

I think, these events also get triggered directly after Screen initialization

Taifun

Hi ABG,
thanks for your response! Read from file will be activated by the button "read" here:
image
Afterwards, when the file was read and "got text" is triggered, the text will be read as variable "name" and JSON-decode to the dictionary: 3 Keys; 3 values...
What are "4" places? I just have 3 text boxes as input and 2 buttons for read and write. For the 3 text boxes I'm using "when File1.GotText" for reading from file and showing into the text boxes, I'm doing a JSONTextDecodeAsDictionaries (3 times!).
image
But the problem will appear when i'm switching after read to anoter text box for inserting a new value. Then suddenly the system stops with described problem.

Or am I completely wrong with my idea?

Ok, let me try with moving the dictionary "set value for key" to the write button function.
I've thought the "screen initialisation" is the only function that will called after opening. Which "events" will get triggered after screen initialization?
I can imagine the variable declaration "initialize global name to" will be triggered by opening screen but all the others should get in ready state but not triggered.
But this will not clearify the behavior of the crash, when I'm switching from one text box entry to another.

...cleaned up the code and changed to instruction from Taifun:


But now the app crashes directly after pressing the "write" button. It seems there is a problem how I'm using the "set value for key" dictionary function. But why?

dict_test (1).aia (3.3 KB)

I can reproduce the problem:

  • starting the app (file idtest2.txt not existend)
  • write in some values in text box 1,2,3
  • press "write" button -> file will be written correctly
  • change some values at text boxes
  • press "read" button -> file will be read correctly and values shown from written file
  • change some values and press "write" again -> now the App chrashes!

Got it working, now!
The solution is just to use 2 different dictionaries. One where dictionary data will be read from file and one with that dictionary will be writen to file. But both must use the same file-name for reading or (over)writing. The synchronisation is just the text box where data is shown from file or written to file.
I do not know the reason but when a written dictionary file ist read, the function "set value for key" will make the app crash.


With this way I'm able to write and read parameter pairs as dictionary...
dict_test (2).aia (3.2 KB)

You can use only one dict with these blocks