小编典典

Flutter测试MissingPluginException

flutter

运行依赖于SharedPreferences插件的测试总是会导致

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

我的pubspec.yaml

dev_dependencies:
  flutter_test:
     sdk: flutter

dependencies:
  flutter:
     sdk: flutter
  shared_preferences: 0.2.3

的代码可以在应用程序本身中正常工作。我是否缺少为运行使用插件的测试而需要做的事情?


阅读 952

收藏
2020-08-13

共1个答案

小编典典

如果您使用的是shared_preferences 0.2.4及更高版本,请使用setMockInitialValues

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

对于早期版本,您可以手动进行操作:

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;
  });
2020-08-13