resource_importer

将资源文件作为字面量导入 Dart 代码。

什么?

resource_importer 最好的描述方式可能是一个例子。您可以将 resource_importer 块添加到 Dart 包的 pubspec.yaml 文件中,其中指定了一些资源文件。

resource_importer:
  resources:
    myImage: 'assets/image.png'
    myLicense:
      path: 'LICENSE'
      type: String

运行 resource_importer 之后,它将生成一个文件(默认命名为 lib/resources.resource_importer.dart),该文件看起来像这样:

var myImage = Uint8List.fromList(const [
  0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00,
  0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x30,
  // ...
]);

var myLicense =
    "Copyright (C) 2022 James D. Lin\n\nThis software is provided 'as-is', ..."

然后,其他 Dart 代码可以 import 该文件。

import 'package:my_package/resources.resource_importer.dart' as resources;

void main() {
  print(resources.myLicense);
}

为什么?

老实说,大多数人可能不应该使用它。Flutter 项目应该使用打包在应用程序中的适当资源。您通常不希望源代码树中包含大型二进制文件的文本表示,这会浪费 Dart 编译器和分析器的时间,并可能浪费您的源代码控制系统空间。(通常您也不应该将生成的文件提交到源代码控制。)

这主要适用于非 Flutter Dart 项目,在这些项目中捆绑附加文件不方便(例如,作为独立可执行文件分发的控制台程序、Dart for the Web 的测试)。

如何做?

配置语法

resource_importer:
  destination: 'lib/foo.resources.dart'
  # Optional.  The path to the generated `.dart` file.  If not specified,
  # `lib/resources.resource_importer.dart` will be used by default.

  resources:
  # Required.  The list of resources to import.

    resourceName:
    # Required.  The name of the resource.  This will be directly used as the
    # name of the generated Dart variable, so it must be a valid Dart
    # identifier.

      path: 'path/to/file'
      # Required.  The path to the file to import.  Relative paths are treated
      # as relative to the package's root directory (i.e., the directory
      # containing the `pubspec.yaml` file).

      type: Uint8List
      # Optional.  The type of the resource.  Corresponds to the type of the
      # generated variable.  Allowed types are:
      #
      # * `Uint8List`
      #     The default if no type is specified.  Imports the specified file
      #     as raw bytes stored in a `Uint8List`.
      #
      # * `String`
      #     Assumes that the specified file is a UTF-8-encoded text file and
      #     imports it as a `String` literal.
      #
      # * `List<String>`
      #     Like `String` except that the imported file is split into separate
      #     lines.
      #
      # * `Base64Data`
      #     Imports a binary file as a base64-encoded `String` literal.
      #
      # * `GzippedData`
      #     Like `Uint8List` but compressed with gzip.

    binaryResourceName: 'path/to/file'
    # A shorthand syntax is also provided for `Uint8List` types.

用法

  1. 修改您的 pubspec.yaml 文件以添加:

    dev_dependencies:
      resource_importer: ^0.1.0
  2. 如上所述,将 resource_importer 块添加到您的 pubspec.yaml 文件中。

  3. 从包含 pubspec.yaml 文件的目录运行 dart run resource_importer 以生成代码。目前这不是自动的,而是期望按需手动运行。

resource_importer 提供的 Base64DataGzippedData 类型是自定义类。如果您使用它们,则必须使用常规依赖项。

dependencies:
  resource_importer: ^0.1.0

然后,分别使用 Base64Data.data()GzippedData.data() 来访问其解码后的字节作为 Uint8List。请注意,GzippedData 依赖于 dart:io,因此不能用于 Dart for the Web。

GitHub

查看 Github