flutter shared_preferences在试图保存字符串时抛出异常

hmae6n7t  于 2023-05-19  发布在  Flutter
关注(0)|答案(2)|浏览(153)

我无法使用iOS的shared_preferences插件保存字符串。

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);

在Android(模拟器和真实的设备)上,此代码运行良好。在iOS上,我遇到了一个例外:

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>

我正在使用shared_preferences: ^2.1.1,XCode 14,Flutter 3.7.12

ruyhziif

ruyhziif1#

问题不在SharedPreferences中。这里检查auth函数和data对象的返回类型是否相同。

Map<String, dynamic> data = await auth(username, password);
krcsximq

krcsximq2#

打开ios/Runner/Info.plist文件,并在<dict>标记中添加以下行:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

相关问题