【问题标题】:How to implement the Plugin Architecture in Flutter Dart如何在 Flutter Dart 中实现插件架构
【发布时间】:2020-02-09 14:24:37
【问题描述】:

我想在 Flutter Dart 中实现一个插件架构。流程如下: 1. 用户下载应用。 2. 用户从我们的网站加载插件。 3. 应用程序查看插件是否实现了接口。 4. 如果实现了接口,则将插件中的信息和小部件加载到应用程序中。

我已经在 C# 中使用在运行时加载已编译的 DLL 实现了相同的过程,但无法为 Flutter 找到它。

我已经查看了一些以前在互联网上可用的问题和资源,我发现最接近的是https://pub.dev/packages/plugins,但该插件在 Dart 2 中不受支持并且已弃用

这是我用 C# 实现的代码。

            int i = 0;

            if (Directory.Exists("Plugins"))
            {
                dllFileNames = Directory.GetFiles("Plugins", "*.dll");

                ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
                foreach (string dllFile in dllFileNames)
                {
                    AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
                    Assembly assembly = Assembly.Load(an);
                    assemblies.Add(assembly);
                }

                Type pluginType = typeof(IPlugin);
                List<Type> pluginTypes = new List<Type>();
                foreach (Assembly assembly in assemblies)
                {
                    if (assembly != null)
                    {
                        Type[] types = assembly.GetTypes();
                        foreach (Type type in types)
                        {
                            if (type.IsInterface || type.IsAbstract)
                            {
                                continue;
                            }
                            else if (pluginType.IsAssignableFrom(type))
                            {
                                pluginTypes.Add(type);
                            }
                        }
                    }

                    i++;
                }

                ICollection<IPlugin> plugins = new List<IPlugin>(pluginTypes.Count);
                foreach (Type type in pluginTypes)
                {
                    IPlugin plugin = (IPlugin)Activator.CreateInstance(type);
                    plugin.Initiate();
                    plugins.Add(plugin);
                }

                return plugins;
            }

            return null;

【问题讨论】:

标签: flutter dart mobile-development plugin-architecture


【解决方案1】:

这可能是不可能的。

为您的应用上传到商店做准备的 AOT 编译的一部分是摇树,删除当前构建不需要的所有内容。所以,你的插件需要调用的任何东西都没有了。

【讨论】:

  • 感谢分享。看完你的回答后,我回到了董事会并提出了一个理论解决方案(截至目前),以在 Flutter 中创建一个体面的插件架构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-02
  • 2020-07-10
  • 2018-04-28
  • 2019-04-03
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
相关资源
最近更新 更多