当我使用楼层数据库时,“MissingPluginException(在通道 com.tekartik.sqflite 上找不到方法 getDatabasesPath 的实现)”

问题描述 投票:0回答:12

我正在尝试使用floor数据库,但是当我想构建数据库时,我遇到了以下错误

E/flutter (26007): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getDatabasesPath on channel com.tekartik.sqflite)
E/flutter (26007): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:154:7)
E/flutter (26007): <asynchronous suspension>
E/flutter (26007): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
E/flutter (26007): #2      invokeMethod (package:sqflite/src/sqflite_impl.dart:17:13)
E/flutter (26007): #3      SqfliteDatabaseFactoryImpl.invokeMethod (package:sqflite/src/factory_impl.dart:82:7)
E/flutter (26007): #4      SqfliteDatabaseFactoryMixin.safeInvokeMethod.<anonymous closure> (package:sqflite_common/src/factory_mixin.dart:25:35)
E/flutter (26007): #5      wrapDatabaseException (package:sqflite/src/exception_impl.dart:7:32)
E/flutter (26007): #6      SqfliteDatabaseFactoryImpl.wrapDatabaseException (package:sqflite/src/factory_impl.dart:78:7)
E/flutter (26007): #7      SqfliteDatabaseFactoryMixin.safeInvokeMethod (package:sqflite_common/src/factory_mixin.dart:25:7)
E/flutter (26007): #8      SqfliteDatabaseFactoryMixin.getDatabasesPath (package:sqflite_common/src/factory_mixin.dart:143:26)
E/flutter (26007): #9      getDatabasesPath (package:sqflite/sqflite.dart:168:54)
E/flutter (26007): #10     DatabaseFactoryExtension.getDatabasePath (package:floor/src/sqflite_database_factory.dart:23:23)
E/flutter (26007): #11     _$AppDatabaseBuilder.build (package:fluttermiwallet/db/database.g.dart:46:40)
E/flutter (26007): #12     main (package:fluttermiwallet/main.dart:9:67)
E/flutter (26007): #13     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:241:25)
E/flutter (26007): #14     _rootRun (dart:async/zone.dart:1184:13)
E/flutter (26007): #15     _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter (26007): #16     _runZoned (dart:async/zone.dart:1619:10)
E/flutter (26007): #17     runZonedGuarded (dart:async/zone.dart:1608:12)
E/flutter (26007): #18     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:233:5)
E/flutter (26007): #19     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (26007): #20     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

我的颤动版本:

Flutter 1.17.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 8af6b2f038 (13 days ago) • 2020-06-30 12:53:55 -0700
Engine • revision ee76268252
Tools • Dart 2.8.4

floor、floor_generator 和 build_runner 的版本如下:

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.3
  floor: ^0.13.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  floor_generator: ^0.13.0
  build_runner: ^1.7.3

main.dart 中,我使用 $FloorAppDatabase 构建数据库:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var db = await $FloorAppDatabase.databaseBuilder('mydb.db').build();
  runApp(MyApp(db));
}

并在 android 文件夹中 GeneratePluginRegistrant.java:

package io.flutter.plugins;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;

/**
 * Generated file. Do not edit.
 * This file is generated by the Flutter tool based on the
 * plugins that support the Android platform.
 */
@Keep
public final class GeneratedPluginRegistrant {
  public static void registerWith(@NonNull FlutterEngine flutterEngine) {
    ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
      io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin.registerWith(shimPluginRegistry.registrarFor("io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin"));
    flutterEngine.getPlugins().add(new io.flutter.plugins.imagepicker.ImagePickerPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin());
    flutterEngine.getPlugins().add(new com.tekartik.sqflite.SqflitePlugin());
  }
}

MainActivity.kt

import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugins.imagepicker.ImagePickerPlugin


class MainActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImagePickerPlugin.registerWith(
                registrarFor("io.flutter.plugins.imagepicker.ImagePickerPlugin"))

    }

}
database flutter sqflite floor
12个回答
33
投票

发生这种情况是由于新添加的依赖项,在我的例子中,以下命令有效。 前往航站楼然后

flutter clean

并重新运行项目


13
投票

愚蠢的我,当我尝试在 Chrome 中运行我的 sqflite 应用程序时遇到了这个错误。当然,sqflite不支持网络,所以我不应该对它抛出错误感到惊讶!在移动模拟器(或 Macos)中运行应该可以。 👌🏻


11
投票

我终于找到答案了。 我必须在我的 MainActivity 中添加 Sqflite 插件:

SqflitePlugin.registerWith(registrarFor("com.tekartik.sqflite.SqflitePlugin"))

我的 MainActivity 更改如下:

import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugins.imagepicker.ImagePickerPlugin
import com.tekartik.sqflite.SqflitePlugin


class MainActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImagePickerPlugin.registerWith(
                registrarFor("io.flutter.plugins.imagepicker.ImagePickerPlugin"))
        SqflitePlugin.registerWith(registrarFor("com.tekartik.sqflite.SqflitePlugin"))

    }

}


10
投票
flutter clean
flutter run

这对我有用。


7
投票

如果您仅在发布版本中遇到此问题,则使用 proguard 规则构建您的发布应用程序

    buildTypes {
        release {
            signingConfig signingConfigs.debug
            minifyEnabled true
            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

android/app/
目录下创建一个文件,命名为
proguard-rules.pro
,并在里面写入:

#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
-keep class androidx.lifecycle.DefaultLifecycleObserver

6
投票

不要使用 HotReload / Run,停止应用程序并重新启动。


3
投票

我会尝试几件事:

  • 首先确保所有插件确实已注册,例如尝试使用 SharedPreferences 而不是 sqflite
  • 遵循插件迁移指南:https://flutter.dev/docs/development/packages-and-plugins/plugin-api-migration如果您的应用程序不是最近创建的
  • 尝试删除 onCreate 中的 ImagePickerPlugin 注册(或者基本上删除 MainActivity 中的 onCreate)。
  • 尝试删除android文件夹并重新创建项目

3
投票

就我而言,我已完成以下步骤以使其正常工作:

  • 在 MainActivity 所在的同一个包中为 Android 创建应用程序类(如果尚未创建)。

我的应用程序.kt

package com.example.flutter_example

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import com.tekartik.sqflite.SqflitePlugin

class MyApp : FlutterApplication(), PluginRegistrantCallback {

    override fun registerWith(registry: PluginRegistry) {
        com.tekartik.sqflite.SqflitePlugin.registerWith(
                registry.registrarFor("com.tekartik.sqflite.SqflitePlugin"))
    }
}
  • 现在更新您的 Android 清单文件并提供您的应用程序类。
<application
        android:name=".MyApp"
  • 现在清理并运行项目。

2
投票

在chrome上运行项目时,程序显示错误是正常的,因为sqflite在chrome上不支持,只需在android/iOs上重新运行项目,错误就会消失。


1
投票

这种情况甚至发生在我身上,只需停止应用程序并重建它,它可能会起作用,因为这意味着依赖项不存在。如果即使这样也不起作用,那么“flutter clean”将完成你的工作


1
投票

我在

Flutter Windows
上遇到此错误,并按照文档解决了。

flutter pub add sqflite_common_ffi

Linux/Windows 用户: https://pub.dev/packages/sqflite_common_ffi

Future<Database>? getDb() {
    _db ??= _initDb();
    return _db;
  }

  // Guaranteed to be called only once.
  Future<Database> _initDb() async {
    Database db;

    if (Platform.isWindows || Platform.isLinux) {
      sqfliteFfiInit();
      var databaseFactory = databaseFactoryFfi;
      db = await databaseFactory.openDatabase(inMemoryDatabasePath);
    } else {
      var databasesPath = await getDatabasesPath();
      final path = '${databasesPath}my_database.db';
      db = await openDatabase(
        path,
        version: 1,
      );
    }
    return db;
  }

0
投票

如果您的 flutter 片段或 Activity 嵌入到 Android 应用程序中,则在启动时只需使用 缓存引擎

仅使用

FlutterFragment
创建的默认引擎似乎无法正确注册插件。

© www.soinside.com 2019 - 2024. All rights reserved.