Reading a non existent key from local state

Hi as from title I want to read a non existent key from the local state. That will return a uint 0. Having said that, I’ll explain my problem. The local state will be of 16 bytes values for necessity. If a certain rare contition is met I store an integer in the local state, so converting it to bytes.
The problem is when the user has not alredy met this condition, so the key does not exist. The uint 0 is returned. Well then what I am achieving to do is to add the value read to a new one (and then restoring in the same key the result). So if the user has not the key, I got the integer 0 and then I can do the sum, converting and storing the bytes. But in the next iteration the local read for that key returns bytes, so I need to convert to int and sum it. If I put this conversion from bytes to int, the smart contract obviously fails at the first iteration, when the key does not exist alredy and the integer 0 is returned.
Except storing permanetly a key, this would mean that 1 lose 1 key out of 16, even if not necessary, can I achieve this in other ways. Would be cool to have a kind of exception handling in teal.
App.localPut(
address,
key,
Itob(Add(
Btoi(App.localGet(address,key)), here the problem
amount
))
)

You can check a key exists using App.localGetEx

https://pyteal.readthedocs.io/en/stable/state.html#state-operation-table

1 Like

Oh it returns a maybe value. Missed it. Thanks as always Fabrice

1 Like