我正在开发一个带有背景内容的Fflight插件.
最近,我遇到了一些关于android_alarm_manager插件中的Flutter android embedding的事情.

README人中有一部分人说:

对于Flatter Android Embedding V1,必须为后台服务提供一个回调,以便在后台服务器上注册插件.

  • Flutter android embeddingV1或V2到底是什么?
  • 这两者有什么不同?

推荐答案

  1. Ffltter发布了新版本的Android Embedding.这是负责将Ffltter集成到Android应用程序中的Android代码.它包括像FlutterActivityFlutterFragmentFlutterViewFlutterEngine这样的类.v2 Android嵌入包括对标准Android生命周期事件的支持,以及将Flutter 执行与Android UI分离,这是v1 Android嵌入所缺少的.在v2Android嵌入的开发过程中,很明显,现有的Flutter 插件API不足以处理v2Android嵌入的新功能.需要一个新的Android插件API.

  2. 在旧的v1Android嵌入中,所有插件都是在Android应用程序的一开始就初始化和配置的,而且只有一次Flutter 体验.在v2嵌入中,我们不假设插件何时初始化,每个FlutterEngine插件必须初始化一次.因此,所有用于安卓的Ffltter插件现在都必须支持实例化,而不是静电初始化,并且它们必须支持附加到FlutterEngine和从FlutterEngine分离.以下代码示例演示了旧的v1插件初始化实现和新的v2插件初始化过程之间的区别.

旧插件初始化

class MyOldPlugin {
  public static void registerWith(PluginRegistrar registrar) {
    // Obtain any references that the plugin requires from the 
    // registrar.
    //
    // This plugin is now considered "initialized" and "attached" 
    // to a Flutter experience.
  }
}

新插件初始化

class MyNewPlugin implements FlutterPlugin {
  public MyNewPlugin() {
    // All Android plugin classes must support a no-args 
    // constructor. A no-arg constructor is provided by 
    // default without declaring one, but we include it here for 
    // clarity.
    //
    // At this point your plugin is instantiated, but it 
    // isn't attached to any Flutter experience. You should not 
    // attempt to do any work here that is related to obtaining 
    // resources or manipulating Flutter.
  }
  @override
  public void onAttachedToFlutterEngine(FlutterPluginBinding binding) {
    // Your plugin is now attached to a Flutter experience 
    // represented by the given FlutterEngine. 
    //
    // You can obtain the associated FlutterEngine with
    // binding.getFlutterEngine()
    //
    // You can obtain a BinaryMessenger with 
    // binding.getBinaryMessenger()
    // 
    // You can obtain the Application context with
    // binding.getApplicationContext()
    //
    // You cannot access an Activity here because this 
    // FlutterEngine is not necessarily displayed within an 
    // Activity. See the ActivityAware interface for more info.
  }
  @override
  public void onDetachedFromFlutterEngine(FlutterPluginBinding binding) {
    // Your plugin is no longer attached to a Flutter experience. 
    // You need to clean up any resources and references that you 
    // established in onAttachedToFlutterEngine().
  }
}

此外,您的插件不能依赖于onAttachedToFlutterEngine()中的活动引用.仅仅因为您的插件附加到Flutter 体验,并不意味着Flutter 体验将在活动中展示.This is one of the most significant differences between the old and new plugin APIs美元.在旧的v1插件API中,插件作者可以依赖于活动立即和永久可用.这已经不是真的了.

有关更多信息,请参见https://medium.com/flutter/modern-flutter-plugin-development-4c3ee015cf5a

Flutter相关问答推荐

自定义ItemBuilderFlutter

出现异常.RangeError(RangeError(index):无效值:有效值范围为空:0).分度误差

修复后期变量的抖动皮棉

如何在容器小部件中的图像旁边添加文本?

Flutter Riverpod,将默认的BTC值改写为我的新状态

Flutter /dart 列表.第一个子类列表中的Where()错误

Android Studio扩展控件上未加载Google map

FittedBox叠加技术在Flutter 中的应用

如何对齐 AppBar Actions 中的文本?

了解多Provider 和流

GridView 渲染项目之间有微小的间隙

不使用 GlobalKey 仍然收到 GlobalKey 被多次使用的错误

更改下拉按钮的背景 colored颜色 - flutter

Flutter:如何在 bottomsheet 中创建一个日期 Select 器,如下图所示

什么最接近 Dart 的 List.asMap().forEach() 返回列表?

如何停止折线图的抖动

如何为文本主题设置多种 colored颜色 ?

如何在 flutter 的 onChanged(更改字符)处更改 TextField 边框 colored颜色 ?

如何在手势检测器中获得涟漪效应

使用 mocktail 包进行单元测试时,类型Null不是Future类型的子类型