Good evening,
Is anyone having the problem with local storages, as I have been crashing for several hours.
My use case is to check if the local storage is empty or not in order to save a value (key, value) and use it in a workflow and create a data in the database.
The problem is that when I do the Do when local storage is empty to add a value, the workflow works whether the condition is true or not.
I have tested all the free plugins, I still have the same problem.
Does local storage have a particular data formatting or not?
Can someone help me please?
All values stored in localStorage are strings (text in Bubble parlance) stored as key/value pairs.
All of the various Bubble plugins that provide interfaces to localStorage that I have looked at basically expect you to only store strings to localStorage and donât seem to do the stringification for you and so donât support different data types in the Bubble sense. And they are often deficient in other ways, which is why I recently introduced my own solution with a plugin called Floppy. Quite simply, Floppy is vastly superior to any other browser storage solution for Bubble.
To solve your problem yourself youâll need to look at whatâs in your appâs localStorage when whatever plugin (or code you yourself are using) writes a value that you think is âemptyâ.
In my intro video for Floppy, I talk about localStorage (and sessionStorage) in general, explain the simple browser interface to it and show how the .setItem() and .getItem() methods work.
Now, this is not explained in the video I post above, but if some key does not exist, .getItem(âsome_keyâ) will return null for that key (null is a special JavaScript object that represents emptiness â the value is not undefined, but thereâs no particular value store there yet). In Bubble, the concept of âemptyâ or âis emptyâ is basically the same and in fact you can basically think of empty and null as the same thing.
However, this doesnât mean that what whatever plugin you are using will store or return for an âemptyâ value. Itâs possible it might store what we call âthe null stringâ â which is the string value ''or "" (a string that has 0 characters in it) or it might store the string value 'null' (that is, a string composed of the letters null).
The latter would happen, for example, if the storage plugin takes âanyâ sort of value and does the writing like this:
The value we would find under some key in localStorage will be the string 'null' if some_value was null (e.g., if you passed the plugin an empty and it allows you to do this).
Similarly, when reading the value back, it depends how the plugin does that. Does it just read the string value back and publish it to some exposed state? Like this:
localStorage.getItem('some key')
or does it parse the value first? Like this (or similar):
JSON.parse(localStorage.getItem('some key'))
If it did that and published the result to an exposed state, instead of finding the string ânullâ at the exposed state, we would find an actual empty (an actual null value).
Again, all of the other plugins Iâve seen for browser storage are rather simplistic and donât give much thought to how actual Bubblers might actually use them. (In the case of Floppy, what I do is let you store actual Bubble values into localStorage, by which I mean we serialize them to strings and then parse them back in (when reading) and publish them back to Bubble not just as strings, but as the actual value in the actual data type that you would expect).
Edit: In Floppy, there are also cases where I simply remove the key rather than storing an âemptyâ value there. For example, if you try to write a scalar key with an empty value, I just donât store a value to the key. If the key exists, I actually remove it. Similarly, for list-type keys, if the length of the list becomes zero (or you try to write an empty value), that key is removed if it previously existed. The reason for this is that the result of .getItem() would be the same in either case, so wy not just clean up the key.
It does a lot of other stuff too. But whether youâre interested in my commercial plugin or not, the video above will likely be helpful to you in understanding browser storage better and understanding how to look at your appâs localStorage and understand whatâs going on with whatever solution you are using at the moment.