29

Running tests which rely on the SharedPreferences Plugin always result in

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

My pubspec.yaml

dev_dependencies:
  flutter_test:
     sdk: flutter

dependencies:
  flutter:
     sdk: flutter
  shared_preferences: 0.2.3

The code for works fine in the application itself. Am i missing something i need to do in order to run tests which make use of a plugin?

MarioNoll
  • 403
  • 1
  • 5
  • 10

4 Answers4

63

If you're using shared_preferences 0.2.4 and above, use setMockInitialValues:

SharedPreferences.setMockInitialValues({}); // set initial values here if desired

For earlier versions you can do it manually:

const MethodChannel('plugins.flutter.io/shared_preferences')
  .setMockMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == 'getAll') {
      return <String, dynamic>{}; // set initial values here if desired
    }
    return null;
  });
Collin Jackson
  • 110,240
  • 31
  • 221
  • 152
  • 1
    I find that the error even occurs when I run SharedPreferences in my .dart file in my lib folder. I had to put the SharedPreferences.setMockInitialValues({}); into the void main() method before runApp and then it worked without the error. – Simon Apr 18 '18 at 08:25
  • I'm using version 0.5.1+2 and this worked for me, thanks. – victommasi Nov 13 '19 at 16:54
  • 4
    Can someone explain why this works? I feel like the old CS meme that says 'my code does not work idk why, now it works, idk why'. – Prashant Ghimire Jun 07 '20 at 22:28
  • 1
    @PrashantGhimire Even I want to ask how this works, it will always delete shared preferences, then why should we use shared preferences – Fayaz Aug 28 '20 at 11:29
  • but this delete all saved values of shared pref – Sambhav jain Mar 05 '21 at 22:38
6

I had the exact same problem with the flutter_secure_storage plugin. I believe the issue is that with both plugins you are relying on storage on your phone or emulator (not something in your app) so it's not available in your test environment. Try running the test directly by executing flutter run your_test_file.dart. According to https://flutter.io/testing/ this should execute your test "in your preferred runtime environment such as a simulator or a device." It worked perfectly for me.

rkunboxed
  • 168
  • 1
  • 10
5

@Siman thanks

version shared_preferences: ^0.5.12

ad SharedPreferences.setMockInitialValues({}); before the runApp() function inside the main funation of Flutter App

makes this error fixed for me

enter image description here

Javeed Ishaq
  • 6,024
  • 6
  • 41
  • 60
  • 3
    It won't solve anything if you're saving logged-in user's data. every time they open the app they'll have log-in – chawila Feb 19 '21 at 14:47
0

A little bit late to the party but if you encounter this problem with any channel or package there is an easy solution. If you can skip testing platform specific issues, you can use a conditional to skip this function call only while testing.

if (!Platform.environment.containsKey('FLUTTER_TEST')) {
  //only runs when you are NOT testing
}
R4dix
  • 11
  • 2