shared preferences not working in the background in flutter
Asked Answered
F

2

10

i am using shared_preferences: ^2.0.7 with flutter_background_service: ^0.1.5 in flutter but when call shared preferences give me the error

"Flutter: Unhandled exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)"

When working in the forground mode, the call is done correctly, this problem occurs only about the call in the background mode.

Festive answered 8/9, 2021 at 10:28 Comment(1)
C
9

same issue's happend with me

in Background : it stores the value (checked by reading value after setting it)

in Foreground : it couldn't find any stored values

Solution

just reload the preference instance before Get & Set

 final prefs = await SharedPreferences.getInstance();
 prefs.reload();  // The magic line 
Cyrie answered 12/3, 2023 at 10:10 Comment(0)
S
1

usually this happens when thirdparty pulgin files are not properly included in build apk , to avoid this try :

flutter clean
flutter pub get
flutter run

this will make sure proper build with required files.

Tip : when adding a new plugin to pubspec.yaml file avoid Hot reload and Hot restart make sure to build apk from scratch so that all required files included in your build.

Slapdash answered 14/7, 2022 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.