Skip to content

[shared_preferences] Crash during use the SharedPreferences, when UserDefaults includes Date type object. #128948

@maciek-atmoapps-com

Description

@maciek-atmoapps-com

Is there an existing issue for this?

Steps to reproduce

  1. Add into to UserDefaults record with type ‚Date’ eg:
    Add in AppDelegate (in application method before ‚return’ statement)
    UserDefaults.standard.set(Date.init(), forKey: "datatest1")
  2. Add shared_preferences dependency into flutter app.
  3. Use SharedPreferences instance with access to all shared preferences.
  SharedPreferences.setPrefix('');
  await SharedPreferences.getInstance();

The issue?

iOS handle following data types in user preferences: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. See NSUserDefaults spec. If UserDefaults include Date type object, application will crash during reading preferences.
Following case can occur especially when migrating from a native app to flutter.

Expected results

App should be not crash.
Key (in user preferences) which included date object should include timestamp value.

Actual results

App crash:

Unsupported value: Thu Jun 15 14:22:20 2023 of type __NSTaggedDate
*** Assertion failure in void WriteValueOfType(CFTypeRef, CFMutableDataRef, FlutterStandardCodecObjcType, CFTypeRef)(), FlutterStandardCodec.mm:341
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unsupported value for standard codec'
*** First throw call stack:
(0x18043fc60 0x197c6bee4 0x181be5de0 0x107a16bf4 0x107a16748 0x184e197cc 0x184c0b18c 0x1803af5d4 0x107a16b44 0x107a16af0 0x107a16748 0x107a183f4 0x107a1491c 0x104afe3cc 0x104aff0d0 0x104afe2c8 0x107a14890 0x10746cb80 0x1800ff094 0x180100094 0x1800acd44 0x1800ac994 0x1803fb014 0x1803b84f8 0x1803cb174 0x1a0ee6988 0x182bce7d8 0x182967c70 0x1048befa4 0x104c804d0)
libc++abi: terminating with uncaught exception of type NSException
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001bacffbbc libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`:
->  0x1bacffbbc <+8>:  b.lo   0x1bacffbd8               ; <+36>
    0x1bacffbc0 <+12>: stp    x29, x30, [sp, #-0x10]!
    0x1bacffbc4 <+16>: mov    x29, sp
    0x1bacffbc8 <+20>: bl     0x1bacfb60c               ; cerror_nocancel
Target 0: (Runner) stopped.
Lost connection to device.
Failed to send request: {"jsonrpc":"2.0","id":"20","method":"getObject","params":{"isolateId":"isolates/682427186373043","objectId":"classes/5801"}}

Code sample

Code sample

AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    UserDefaults.standard.set(Date.init(), forKey: "datatest1")
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences.setPrefix('');
  await SharedPreferences.getInstance();
  runApp(const MyApp());
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.10.5, on macOS 13.3.1 22E261 darwin-x64, locale pl-PL)
    • Flutter version 3.10.5 on channel stable at /Users/maciek/development/sdks/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (2 days ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/maciek/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • ANDROID_HOME = /Users/maciek/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.78.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.64.0

[✓] Connected device (4 available)
    • Pixel 6 (mobile)         • 18241FDF600335                           • android-arm64  • Android 13 (API 33)
    • Marcin’s iPhone (mobile) • e73d6caac7f8be993c64c168731a1809fe5e1c91 • ios            • iOS 15.7.6 19H349
    • macOS (desktop)          • macos                                    • darwin-x64     • macOS 13.3.1 22E261 darwin-x64
    • Chrome (web)             • chrome                                   • web-javascript • Google Chrome 114.0.5735.106

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

• No issues found!

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listc: new featureNothing broken; request for a new capabilityp: shared_preferencesPlugin to read and write Shared Preferencespackageflutter/packages repository. See also p: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions