skip to Main Content

I can not save a string using shared_preferences plugin for iOS.

Future<Map<String, dynamic>> login(String username, String password) async {
    Map<String, dynamic> data = await auth(username, password);

    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('username', username);

On Android (both emulator and real device) this code works fine. On iOS however, I encounter an exception:

A Dart VM Service on iPhone 14 Pro Max is available at:
http://127.0.0.1:61586/Rp0YW0x-ZBY=/
The Flutter DevTools debugger and profiler on iPhone 14 Pro Max is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:61586/Rp0YW0x-ZBY=/
[VERBOSE-2: dart_vm_initializer.cc(41)] Unhandled Exception: type Map<Object?, Object?>' is not a subtype of type 'List<Object?>?' in type cast
#0
UserDefaultsApi.setValue (package:shared_preferences_foundation/messages.g.dart:95:59)
<asynchronous suspension>
#1
SharedPreferences Foundation.setValue (package:shared_preferences_foundation/shared_preferences_foundation.dart: 77:5)
<asynchronous suspension>
#2
UserProvider.login (package:vks_flutter_app/providers/user_provider.dart :32:5)
<asynchronous suspension>

I am using shared_preferences: ^2.1.1, XCode 14, Flutter 3.7.12

2

Answers


  1. The problem is not in SharedPreferences. Check if the return type of the auth function and the data object are the same here.

    Map<String, dynamic> data = await auth(username, password);
    
    Login or Signup to reply.
  2. Open the ios/Runner/Info.plist file and add the following lines inside the <dict> tag:

    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search