Search code examples
flutterdartsharedpreferences

How to fix: No implementation found for method getAll on channel plugins.flutter.io/shared_preferences


I have been going at this problem for almost half a day now. I am trying to use Flutter's SharedPreferences package to store key/value in app's storage. When I go to new-up a SharedPreferences through await SharedPreferences.getInstance(), it builds and runs properly, but when the app reaches that line of code, it crashes with the following error:

MissingPluginException (MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences))

I have found other Stackoverflow posts, but they have not helped resolve this isue:

Through all of these, I have tried the following:

  • flutter pub get
  • io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin.registerWith(registry.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));
  • Since I am on iOS: SharedPreferencesIOS.registerWith();
  • https://pub.dev/packages/flutter_isolate
  • flutter clean && flutter pub get
  • SharedPreferences.setMockInitialValues({});

After running/doing the above steps, I restarted my editor (VSC). I even installed Android Studio with the Flutter extension and tried it out and it did not work. I tried running from the command line, but it still returns the same error. I tried restarting my computer and uninstalling and reinstalling flutter and dart for laughs. I really do not know what to do at this point. What steps am I missing?


Solution

  • Add this to your code before you use shared preferences.

    SharedPreferences.setMockInitialValues({});
    

    It is because if you use getAll where there is nothing, it will go crazy. I don't think it is anything to do with iOS. If you even use normal getString, the internal program uses getAll so it will still crash.

    What you can do to fix that is put it in a try and catch statement. In try, call sharedPreferences.get and catch the error and then do setMockInitialValues. If sharedPreferences.get does not give an error, it means there is already datai I am not sure. can you try.