localStorage returns NULL
Working Live example.
You've to parse the returned string to JSON to get the station
like :
var defaultReportData = JSON.parse(localStorage.getItem('defaultReportData'));
console.log( defaultReportData.station );
Getting items from localstorage returns null
localStorage.setItem(key, value)
expecting a value to be string. When you pass the object such as [{id: 1}]
, it will typecast it to string. Hence the object becomes the string like this "[object Object]"
.
localStorage.setItem('test', [{id: 1}]);
const item = localStorage.getItem('test');
console.log(item) // "[object Object]" a string
console.log(item[0]) // "["
console.log(item[1)) // "o"
Solution
The solution is to stringify it before saving to localStrage and parse it after getting the item.
localStorage.setItem('workItems', JSON.stringify(response.data.items));
// when you get the items
if (JSON.parse(localStorage.getItem('workItems')).length > 0)
Flutter localstorage.getItem() returns null
var ready = await storage.ready;
print('searching ${key} is $ready');
dynamic userJson = storage.getItem(key);
Just check storage.ready and search item, that's it.
Related Topics
Change Name of Uploaded File on Client
How to Pass JavaScript Confirm Variable to PHP Variable
How to Resolve the C:\Fakepath
Uncaught Typeerror: (Intermediate Value)(...) Is Not a Function
How to Format a Float as Integer When .Tofixed(2) Gives Zeros After the Decimal Point
How to Build Pdf File from Binary String Returned from a Web-Service Using JavaScript
Html5 and JavaScript to Play Videos Only When Visible
How to Only Remove the "Watch Later" and "Share" Buttons from Youtube Iframe Embed Player
Why Is the Onclick Event Triggered Twice
How to Save an Image to Localstorage and Display It on the Next Page
Discord.Js Ban/Kick Commands Available to All Users. How to Fix This
Filtering Item from Nested Object Array in Typescript
What Is the Purpose of the Dollar Sign in JavaScript
Javascript Validation: Block Special Characters
Detect If String Contains Any Spaces
React Enable Button After All Form Fields Are Not Empty
How to Prevent iOS Keyboard from Pushing the View Off Screen With CSS or Js