SharedPreferences error in Flutter
Asked Answered
B

7

7

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"

Bellyband answered 21/3, 2018 at 13:11 Comment(7)
github.com/flutter/flutter/issues/10872Web
I had this problem using the Dart 2.0 preview. I guess that should be fixed using shared_preferences 0.4.0, which version are you using?Selfcongratulation
@Selfcongratulation I am using shared_preferences: "^0.4.0"Bellyband
@NileshRathod Tried to change to "0.2.4" as you suggested, no differenceBellyband
No idea if this will help, but perhaps you could try doing flutter clean as well as pub cache repair to see if you can shake things outPersona
pub cache repair shouldn't be necessary. I need flutter clean frequently though to ensure changes take effect.Nereus
Tried to pub cache repair and flutter clean, nothing helpedBellyband
D
3

I've tried shared_preferences: "0.2.4" and other versions suggested above without any success. Finally got it to work after changing flutter channel from dev to beta:

flutter channel beta

At least this fixes this problem for now and just wait for a fix for the shared_preferences plugin on the dev channel.

Dissociate answered 30/3, 2018 at 1:33 Comment(1)
I tried this but get the message git: Already on 'beta' and I am already using 0.2.4 Still get the same error.Abdu
I
7

I solved this using the following workaround:

Future<SharedPreferences> prefs = SharedPreferences.getInstance();
prefs.then(
  (pref)
  {
    //call functions like pref.getInt(), etc. here
  }
);
Inflation answered 28/7, 2018 at 14:23 Comment(0)
E
6

To debug this, use the following:

Future<SharedPreferences> _sprefs = SharedPreferences.getInstance();
_sprefs.then((prefs) {
  // ...
}, 
onError: (error) {
  print("SharedPreferences ERROR = $error");   
});

In my case the error was that I wanted to call await SharedPreferences.getInstance() before I called runApp(), so that solution the error message gave me was to order my code as follows:

First:

WidgetsFlutterBinding.ensureInitialized();

Afterwards:

SharedPreferences prefs = await SharedPreferences.getInstance();

Finally:

runApp(...);
Eigenvalue answered 21/6, 2020 at 2:56 Comment(0)
D
3

I've tried shared_preferences: "0.2.4" and other versions suggested above without any success. Finally got it to work after changing flutter channel from dev to beta:

flutter channel beta

At least this fixes this problem for now and just wait for a fix for the shared_preferences plugin on the dev channel.

Dissociate answered 30/3, 2018 at 1:33 Comment(1)
I tried this but get the message git: Already on 'beta' and I am already using 0.2.4 Still get the same error.Abdu
M
2

I fixed it by changing to shared_preferences: "0.3.3". There is a good chance this breaks again.

Mackenziemackerel answered 25/4, 2018 at 0:8 Comment(2)
I still get the same error after changing to 0.3.3Abdu
@Guus, yeah, not exactly sure why the fix worked for us. We ended up switching our entire state management to Redux. If you want to continue using shared_preferences, maybe try some other version numbers to see if they fix the issue. Sorry I can't give you a better answer.Mackenziemackerel
N
0

you need to use Future like this

Future<SharedPreferences> _sprefs = SharedPreferences.getInstance();
Nickolai answered 5/5, 2018 at 18:4 Comment(1)
Need a more complete example. Can you show code to get a preference, please?Scotland
M
0

I remove packages one by one, and found that it was caused by flutter_barcode_scanner. I upgraded it to version 2.0.0 and it solved my problem.

Mauro answered 18/1, 2022 at 9:9 Comment(0)
W
0

It was working fine earlier but now I am getting invalid format exception while calling SharedPreferences.getInstance();

and for solution I tried everything but nothing work.so at last I create new project and paste all my previous code. and that's finally solve the problem

Whitening answered 19/12, 2023 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.