我想在 Flutter Dart 中实现一个插件架构。流程如下:
1. 用户下载应用。
2. 用户从我们的网站加载插件。
3. 应用程序查看插件是否实现了接口(interface)。
4. 如果实现了接口(interface),则将信息和小部件从插件加载到应用程序。
我已经在 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;
最佳答案
这可能是不可能的。
准备您的应用程序以上传到商店的 AOT 编译的一部分是摇树,删除当前构建不需要的所有内容。因此,您的插件需要调用的任何东西都消失了。
关于flutter - 如何在 Flutter Dart 中实现插件架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58351739/