Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences error in Flutter

I was using shared_preferences plugin in my Flutter application. From one moment (probably after Flutter upgrade), it started to throw an exception:

E/flutter (27778): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (27778): type '_InternalLinkedHashMap' is not a subtype of type 'Map<String, Object>' where
E/flutter (27778):   _InternalLinkedHashMap is from dart:collection
E/flutter (27778):   Map is from dart:core
E/flutter (27778):   String is from dart:core
E/flutter (27778):   Object is from dart:core
E/flutter (27778): 
E/flutter (27778): #0      SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart)
E/flutter (27778): <asynchronous suspension>
E/flutter (27778): #1      loadFirstUse (**path**/lib/main.dart:29:53)
E/flutter (27778): <asynchronous suspension>
E/flutter (27778): #2      main (**path**/lib/main.dart:17:9)
E/flutter (27778): <asynchronous suspension>
E/flutter (27778): #3      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
E/flutter (27778): #4      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)

It happens when I simple try to create instance of SharedPreferences:

SharedPreferences prefs = await SharedPreferences.getInstance();

I was trying to find root of the problem, but was unable to find it. Thank you for any help.

EDIT: I am using shared_preferences: "^0.4.0"

like image 936
Tony Danilov Avatar asked Mar 21 '18 13:03

Tony Danilov


1 Answers

I solved this using the following workaround:

Future<SharedPreferences> prefs = SharedPreferences.getInstance();
prefs.then(
  (pref)
  {
    //call functions like pref.getInt(), etc. here
  }
);
like image 131
Roy Avatar answered Sep 19 '22 04:09

Roy