dart - Flutter 测试 MissingPluginException

运行依赖于 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

的代码在应用程序本身中运行良好。 为了运行使用插件的测试,我是否遗漏了一些我需要做的事情?

最佳答案

如果您使用的是 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;
  });

https://stackoverflow.com/questions/44357053/

相关文章:

flutter - 如何在不改变 AppBar 的情况下更改 TabBar 的背景颜色?

android - flutter 上的图像比例类型中心裁剪?

flutter - 为什么某些变量在 flutter 自定义类中标记为最终变量?

visual-studio-code - VS Code(OSX)Flutter热重载不是由保存触发

firebase - 从 Flutter 中的 Firestore 集合中获取所有内容

ios - flutter IOS : CocoaPods could not find compa

android - 如何在 Flutter 中创建可扩展的 ListView

dart - 使用 Flutter/Dart 关闭 Dismissible

http - 在flutter http请求中为所有请求设置默认 header 的最佳方法

animation - 在 Flutter 中将 ListView 项目动画化为全屏