Flutter Test MissingPluginException
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?
Solution 1:
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;
});
Solution 2:
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.
Solution 3:
@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