Skip to content

[shared_preferences] On Web, exception on initialization when a stored value is not valid JSON #156574

@gdurandrexel

Description

@gdurandrexel

Steps to reproduce

Create a web project.
In my case, use secure_storage to store some value. It is stored as plain text.

Expected results

The value that cannot be decoded should be ignored.

Actual results

When trying to initialize from this local storage:

image

I get this exception:

[2024-10-11 11:01:16.394 | Catcher 2 | INFO] ---------- ERROR ----------
[2024-10-11 11:01:16.396 | Catcher 2 | INFO] FormatException: SyntaxError: Unexpected token 'k', "k6IlhTmQIL"... is not valid JSON
[2024-10-11 11:01:16.396 | Catcher 2 | INFO]
[2024-10-11 11:01:16.397 | Catcher 2 | INFO] ------- STACK TRACE -------
[2024-10-11 11:01:16.737 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3  throw_
[2024-10-11 11:01:16.737 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/convert_patch.dart 36:5          _parseJson
[2024-10-11 11:01:16.738 | Catcher 2 | INFO] dart-sdk/lib/convert/json.dart 610:36                                        convert
[2024-10-11 11:01:16.738 | Catcher 2 | INFO] dart-sdk/lib/convert/json.dart 216:41                                        decode
[2024-10-11 11:01:16.738 | Catcher 2 | INFO] packages/shared_preferences_web/shared_preferences_web.dart 262:37           _decodeValue
[2024-10-11 11:01:16.739 | Catcher 2 | INFO] packages/shared_preferences_web/shared_preferences_web.dart 135:22           _readAllFromLocalStorage
[2024-10-11 11:01:16.739 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54           runBody
[2024-10-11 11:01:16.739 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5           _async
[2024-10-11 11:01:16.739 | Catcher 2 | INFO] packages/shared_preferences_web/shared_preferences_web.dart 129:55           [_readAllFromLocalStorage]
[2024-10-11 11:01:16.740 | Catcher 2 | INFO] packages/shared_preferences_web/shared_preferences_web.dart 126:12           getPreferences
[2024-10-11 11:01:16.740 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54           runBody
[2024-10-11 11:01:16.740 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5           _async
[2024-10-11 11:01:16.740 | Catcher 2 | INFO] packages/shared_preferences_web/shared_preferences_web.dart 122:45           getPreferences
[2024-10-11 11:01:16.741 | Catcher 2 | INFO] packages/shared_preferences/src/shared_preferences_async.dart 55:22          getAll
[2024-10-11 11:01:16.741 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54           runBody
[2024-10-11 11:01:16.741 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5           _async
[2024-10-11 11:01:16.742 | Catcher 2 | INFO] packages/shared_preferences/src/shared_preferences_async.dart 52:38          getAll
[2024-10-11 11:01:16.742 | Catcher 2 | INFO] packages/shared_preferences/src/shared_preferences_async.dart 225:32         reloadCache
[2024-10-11 11:01:16.742 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54           runBody
[2024-10-11 11:01:16.743 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5           _async
[2024-10-11 11:01:16.743 | Catcher 2 | INFO] packages/shared_preferences/src/shared_preferences_async.dart 222:27         reloadCache
[2024-10-11 11:01:16.743 | Catcher 2 | INFO] packages/shared_preferences/src/shared_preferences_async.dart 200:22         create
[2024-10-11 11:01:16.744 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54           runBody
[2024-10-11 11:01:16.744 | Catcher 2 | INFO] dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5           _async
[2024-10-11 11:01:16.744 | Catcher 2 | INFO] packages/shared_preferences/src/shared_preferences_async.dart 187:51         create

Code sample

Code sample
final sharedPreferences = await SharedPreferencesWithCache.create(
      cacheOptions: const SharedPreferencesWithCacheOptions());

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.24.3, on Microsoft Windows [version 10.0.22631.4169], locale fr-FR)
    • Flutter version 3.24.3 on channel stable at c:\Vrac\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (4 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\gdurand\AppData\Local\Android\Sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0--11609105)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.11.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.11.35327.3
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2024.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0--11609105)

[√] IntelliJ IDEA Community Edition (version 2020.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.1
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart

[√] VS Code (version 1.94.1)
    • VS Code at C:\Users\gdurand\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.98.0

[√] Connected device (4 available)
    • SM G930U (mobile) • b86bd847 • android-arm64  • Android 8.0.0 (API 26)
    • Windows (desktop) • windows  • windows-x64    • Microsoft Windows [version 10.0.22631.4169]
    • Chrome (web)      • chrome   • web-javascript • Google Chrome 129.0.6668.90
    • Edge (web)        • edge     • web-javascript • Microsoft Edge 129.0.2792.65

[√] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.platform-webWeb applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-webOwned by Web platform teamtriaged-webTriaged by Web platform team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions