原有android项目集成flutter运行报错MissingPluginException(No implementation found for method getAll on channel

项目场景:

原有android项目集成flutter,
环境:flutter sdk : 1.17.4,
android studio :3.6.3,
android sdk : 29


问题描述:

运行报错:[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)


解决方案:

如果您使用的是shared_preferences 0.2.4及更高版本,可直接在flutter项目的main方法中添加以下内容:

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;
  });