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

I am trying to use shared preferences in a background handler (when receiving a notification and using Firebase Cloud Messaging). However, I get the error message that it could not find an implementation of shared_preferences. I read on Github that in the beta version you have 2 options.

Option 1

DartPluginRegistrant.ensureInitialized();

The above is only for Flutter 2.11+ and is not in the release version yet.

Option 2

  1. Add dependencies on shared_preferences_android and shared_preferences_ios.
  2. At the beginning of your background isolate entry point, add something like:
if (Platform.isAndroid) SharedPreferencesAndroid.registerWith();
if (Platform.isIOS) SharedPreferencesIOS.registerWith();

I want to go for option 2, but I do not know where to put those lines of code. Also, does this mean I have to add 2 new dependencies? I already have the original shared preferences package installed.

Here is the Github Flutter issue: https://github.com/flutter/flutter/issues/98473#issuecomment-1041895729

Thanks for your help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source