如何在 Flutter Dart 中实现插件架构

Tec*_*eak 5 plugin-architecture dart mobile-development flutter

我想在 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;
Run Code Online (Sandbox Code Playgroud)

Ran*_*rtz 0

这可能是不可能的。

准备应用程序上传到商店的 AOT 编译的一部分是进行树摇动,删除当前构建不需要的所有内容。因此,您的插件需要调用的任何内容都消失了。