简体   繁体   English

直接在 flutter 项目中使用插件,无需分离到另一个 package 或在 pubspec.yaml 中添加插件字段

[英]Use plugin directly inside flutter project without separating to another package or adding plugin field in pubspec.yaml

I want to create a plugin to use in my project.我想创建一个插件以在我的项目中使用。 I wonder that how I can call or setup it for invoking from flutter application without specific it in pubspec.yaml (because I use some other packages which also use their owned plugins, if I specify my plugin inside pubspec.yaml, those plugins do not work) or separating to another package.我想知道如何调用或设置它以从 flutter 应用程序调用而无需在 pubspec.yaml 中指定它(因为我使用其他一些也使用它们自己的插件的包,如果我在 pubspec.yaml 中指定我的插件,那些插件不会工作)或分离到另一个 package。

Here is my current code:这是我当前的代码:

class DemoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
    private lateinit var channel: MethodChannel
    private lateinit var context: Context
    private lateinit var activity: Activity

    override fun onMethodCall(call: MethodCall, result: Result) {
        when (call.method) {
            "demo" -> {
                result.success("Demo")
            }
        }
    }

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPluginBinding) {
        context = flutterPluginBinding.applicationContext
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, channelName)
        channel.setMethodCallHandler(this)
    }

    override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        activity = binding.activity
    }

    override fun onDetachedFromActivityForConfigChanges() {}

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {}

    override fun onDetachedFromActivity() {}
}

According to the docs of the FlutterPlugin Class you can do this in the MainActivity Class.根据 FlutterPlugin Class 的文档,您可以在 MainActivity Class 中执行此操作。

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        flutterEngine.plugins.add(DemoPlugin())  // add the plugin
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在“pubspec.yaml”中找不到“flutter.plugin.platforms”键 - Cannot find the `flutter.plugin.platforms` key in the `pubspec.yaml` 在 pubspec.yaml 中使用 fluttertoast 时 flutter 构建问题 - flutter build issue when use fluttertoast in pubspec.yaml flutter pubspec.yaml 退出代码 65 - flutter pubspec.yaml exit code 65 Flutter pubspec.yaml Android版本代码 - Flutter pubspec.yaml Android versionCode 错误:找不到 pubspec.yaml 文件。 此命令应从 Flutter 项目的根目录运行 - Error: No pubspec.yaml file found. This command should be run from the root of your Flutter project 找不到 pubspec.yaml 文件。 此命令应从 Flutter 项目的根目录运行 - No pubspec.yaml file found. This command should be run from the root of your Flutter project 为什么在 pubspec.yaml 中添加 firebase_admob: ^0.11.0+1 运行时会产生错误(Flutter,Android) - Why adding firebase_admob: ^0.11.0+1 in pubspec.yaml generats error when run (Flutter, Android) 我想在flutter应用程序中添加字体,但pubspec.yaml中有问题 - i want to add fonts in flutter app, but there is a problem in pubspec.yaml 在flutter中,如何将tfl​​ite添加为pubspec.yaml文件中的依赖项? - In flutter, How to add tflite as a dependency in pubspec.yaml ?file? 无法在 flutter 中安装来自 pubspec.yaml 的 firebase 依赖项? - Can't install firebase dependencies from pubspec.yaml in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM