flutter_localstorage: Error on init - Unexpected end of input (at character 1) ^

99/100 times the code we use to start our app up, everything works. Every once in a great while- we see this error and the app hangs.

Non-fatal Exception: FormatException: Unexpected end of input (at character 1) ^
thrown null

Here is a look at the stack I am seeing in crashlytics. Screen Shot 2020-05-21 at 5 24 08 PM

The code I run to initiate this is await _storage.ready

I threw up a PR but it is more investigative in nature. I have noticed json.encode(“”) or json.encode(null) does throw the error Unexpected end of input (at character 1) ^

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 5
  • Comments: 38 (22 by maintainers)

Commits related to this issue

Most upvoted comments

please upgrade to v3.0.4+7

@nohli thanks for your test app again, it was very helpful for me. FYI: this issue is no longer reproducible in your test app 🎉

@nohil thank you very much! I will check the app you’ve created

@nohli that is an interesting observation…

Am I getting this correct that after you had something saved in v3.0.3 and upgraded to v3.0.4 it wasn’t able to save the state again?

Edit: Fixed in 3.0.6+9.

I think, 3.0.4 saved incorrectly. And regardless of the version, it wasn’t able to load again (like the end was missing and json.decode() throws an error):

But as I wrote earlier, it was possible to get the whole string from the file before json.decode() and correct the string…

please upgrade to v3.0.4+7

@nohli thanks for your test app again, it was very helpful for me. FYI: this issue is no longer reproducible in your test app 🎉

Great!

Thanks for fixing the issue. Now I can sleep better 😛 And thanks for this package!

Thanks @nicoroy2561

I have a question related to the performance of setItem(), which may help indirectly with this issue.

For each call to setItem(), the localstorage library does json.encode(), json.decode(), and json.encode() again. The first json.encode() is in localstorage.dart and the second is in the underlying DirUtils classes in io.dart and web.dart.

I know JSON encode/decode can be expensive operations, and in my app we are writing a lot of data each time. Is it necessary to encode->decode->encode like this?

Thanks for any help with this!

I’m taking a break, will be back on Monday and have a closer look. sorry for taking so long 😔