简体   繁体   English

如何为Flutter应用创建纯Dart程序包?

[英]How do I create a pure Dart package for a Flutter app?

I am trying to create a pure Dart package to be used in my Flutter app. 我正在尝试创建一个纯Dart程序包以在我的Flutter应用程序中使用。 Problem is that when building my app some Android and iOS specific files are generated under the library folder. 问题是,在构建我的应用程序时,库文件夹下会生成一些特定于Android和iOS的文件。 I wonder if this is a bug or a known feature? 我想知道这是错误还是已知功能?

I have created the project using the command: 我已经使用以下命令创建了项目:

flutter create -t package mypackage

For example the generated file GeneratedPluginRegistrant.java looks like this: 例如,生成的文件GeneratedPluginRegistrant.java如下所示:

package io.flutter.plugins;

import io.flutter.plugin.common.PluginRegistry;

/**
 * Generated file. Do not edit.
 */
public final class GeneratedPluginRegistrant {
  public static void registerWith(PluginRegistry registry) {
    if (alreadyRegisteredWith(registry)) {
      return;
    }
  }

  private static boolean alreadyRegisteredWith(PluginRegistry registry) {
    final String key = GeneratedPluginRegistrant.class.getCanonicalName();
    if (registry.hasPlugin(key)) {
      return true;
    }
    registry.registrarFor(key);
    return false;
  }
}

Can I avoid generating those GeneratedPluginRegistrant.* files? 我可以避免生成那些GeneratedPluginRegistrant。*文件吗? If not, do I have to commit them to source control? 如果不是,是否必须将它们提交给源代码管理?

For pure dart packages, you need only one thing: a pubspec.yaml , with a name & environment constraints: 对于纯dart包,您只需要一件事情: pubspec.yaml ,它具有名称和环境限制:

name: my_package
environment:
  sdk: ">=2.0.0 <3.0.0"

This is enough to add my_package as a dependency to your flutter project though: 不过,这足以将my_package添加为您的flutter项目的依赖项:

dependencies:
  my_package:
    path: ../path/to/my_package/folder

You can do it with command line: 您可以使用命令行执行此操作:

  1. path to pub should be set in your environment variable like: pub路径应在您的环境变量中设置,例如:

    C:\\Users\\UserName\\AppData\\Roaming\\Pub\\Cache\\bin; C:\\ Users \\ UserName \\ AppData \\ Roaming \\ Pub \\ Cache \\ bin;

  2. pub global activate stagehand

  3. mkdir dart_project

  4. cd dart_project

  5. stagehand package-simple

Generating it with command line will generate a bit of code for you which will give you an idea that how a package should be managed. 使用命令行生成它会为您生成一些代码,这将使您了解应如何管理软件包。

You can use this project as dependency in pubspec.yaml file of your Flutter or Angular-Dart project as: 您可以将此项目用作Flutter或Angular-Dart项目的pubspec.yaml文件中的依赖项,如下所示:

dependencies:
  dart_project:
    path: ../path/to/dart_project

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM