Flutter中open_file_plus编译失败:PluginRegistry.Registrar找不到符号
Flutter 应用内创建并打开PDF时出现PluginRegistry.Registrar构建错误
我正在开发一款Flutter移动应用,需要在应用内创建PDF后打开它。使用最新版本open_file_plus: ^3.4.1+1时,编译始终出现PluginRegistry.Registrar相关构建错误。
保存并打开PDF的代码
openFile() async { String storeNameForInvoice = '${storeName.value} - $date'; final invoicesPath = await getTargetDirectoryPath(); final file = File("$invoicesPath/$storeNameForInvoice.pdf"); await file.writeAsBytes(await pdf.save()); await saveInvoiceToFirestore(); loadInvoicesToCurrentInvoice(storeName.value); await OpenFile.open(file.path); }
配置详情
1. Gradle版本 (gradle-wrapper.properties)
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip
2. 编译与Target SDK版本 (build.gradle 应用级)
compileSdk = 34 targetSdk = 34
3. Settings.gradle配置
pluginManagement { def flutterSdkPath = { def properties = new Properties() file("local.properties").withInputStream { properties.load(it) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" return flutterSdkPath }() includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version "7.3.0" apply false id "com.google.gms.google-services" version "4.3.15" apply false id "org.jetbrains.kotlin.android" version "1.9.0" apply false } include ":app"
错误信息
C:\Users\MQ\AppData\Local\Pub\Cache\hosted\pub.dev\open_file_plus-3.4.1+1\android\src\main\java\com\joutvhu\openfile\OpenFilePlusPlugin.java:66: error: cannot find symbol public static void registerWith(PluginRegistry.Registrar registrar) { ^ symbol: class Registrar location: interface PluginRegistry 1 error FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':open_file_plus:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: > Run with --info option to get more log output. > Run with --scan to get full insights. BUILD FAILED in 1m 20s Error: Gradle task assembleDebug failed with exit code 1
补充信息
已尝试flutter upgrade、flutter clean、flutter pub upgrade、flutter pub outdated等操作,但问题仍存在。测试open_file、url_launcher等同类库也出现相似错误。
问题
- PluginRegistry.Registrar错误的成因是什么?
- 如何解决该问题以成功在Flutter应用中打开PDF?
解答
错误成因
PluginRegistry.Registrar是Flutter旧版插件API的接口,在Flutter 3.10+版本中,该接口已被废弃并移除,改用新的FlutterPlugin API实现插件注册。你使用的open_file_plus: ^3.4.1+1版本的Android端代码仍在使用旧的注册方式,与当前Flutter版本不兼容,导致编译报错。
解决方法
方法一:升级到兼容新版Flutter的插件版本
检查open_file_plus的最新版本,确认是否已适配新版Flutter插件API。在pubspec.yaml中更新依赖:
dependencies: open_file_plus: ^最新兼容版本
更新后执行flutter pub get,再重新编译项目。
方法二:修改插件的Android代码(临时方案)
如果暂时没有兼容版本,可以手动修改本地缓存的插件代码:
- 打开错误提示中的文件路径:
C:\Users\MQ\AppData\Local\Pub\Cache\hosted\pub.dev\open_file_plus-3.4.1+1\android\src\main\java\com\joutvhu\openfile\OpenFilePlusPlugin.java - 删除旧的
registerWith方法,替换为新版Flutter插件注册逻辑:
import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; public class OpenFilePlusPlugin implements FlutterPlugin, MethodCallHandler { private MethodChannel channel; @Override public void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding flutterPluginBinding) { channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "open_file_plus"); channel.setMethodCallHandler(this); } @Override public void onMethodCall(MethodCall call, Result result) { // 保留原有的方法处理逻辑 } @Override public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) { channel.setMethodCallHandler(null); } }
- 同时修改
android/src/main/AndroidManifest.xml,移除旧的<meta-data>注册项(如果有),确保插件通过新方式注册。
方法三:使用替代插件
如果上述方法无效,可以尝试使用已适配新版Flutter的文件打开插件,比如filex或open_filex,这些插件通常已更新到新的插件API。
内容的提问来源于stack exchange,提问作者Muhammad Qazmouz
相关产品推荐
相关产品推荐

