搜索插件、代码仓与 APP 分类

插件详情v4.1.2

index_generator

自动为您的库生成索引/条形/库文件,包含所有所需的导出。

AndroidiOSmacOSWindowsLinux
近 30 天下载
2.6万
喜欢数
33
Pub 分数
140

安装插件

flutter pub add index_generator

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

index_generator 是一个 Flutter/Dart 包,可自动创建索引(barrel)文件,通过为所有相关的 Dart 文件生成导出,简化库的组织。它减少了在管理包含多个模块或包的大型代码库时的手动工作量,并降低了出错的可能性。该工具可通过命令行无缝集成到开发工作流中,确保跨目录的导出声明保持一致且最新。非常适合注重代码结构清晰、可维护性的开发者。

适合场景

  • 自动化导出文件生成
  • 保持库结构的整洁
  • 简化包开发
  • 减少手动导出错误
  • 支持模块化应用架构

主要特点

  • 自动生成 barrel 文件
  • 命令行集成
  • 支持多个平台
  • 确保一致的导出
  • 所需配置最少

适合对象

  • 大规模 Flutter 项目
  • 库作者
  • 使用模块化代码库的团队
  • 寻求自动化的开发者
  • 频繁文件重构的项目

使用建议

  • 需要在 pubspec.yaml 中进行设置
  • 可能与手动编辑冲突
  • 最佳使用于受控的目录结构中
  • 不适合简单应用
  • 依赖于正确的文件夹组织

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Index Generator

Automatically generate index / barrel files with all the exports needed for your library.

Stable release

There will be no more breaking changes

Usage

  1. Activate: dart/flutter pub global activate index_generator

  2. Create index_generator.yaml file or add in your pubspec.yaml file:

index_generator:
  exclude:
    - "**.g.dart"
  # Define the paths of the folders in which to generate the index files
  libraries:
    - directory_path: lib
  1. You can generate index files with: <dart|flutter> pub global run index_generator

Advance Usage

See more examples

index_generator:
  # You can define the default name of the index file
  default_file_name: barrel
  # You can define general filters for all indexes
  exclude:
    - "**.g.dart"
    - "{_,**/_}*.dart"
  libraries:
    - directory_path: lib
      # You can define specific filters for this index
      include:
        # You can define specific export folders paths
        - "src/**"
      # You can define specific export dart packages in index file.
      exports:
        - package: package:args/args.dart
          show:
            - ArgResults
          hide:
            - ArgParser
    - directory_path: generator
      # You can define the name of the index file
      file_name: main
      # Disable/Enable the generated code disclaimer
      disclaimer: false
      # Include/Exclude the library keyword in generated file
      include_library: true
      # Comments added on the generated file, like copyright
      comments: |
        Copyright (c) 2020 BreX900
      # Documentation added on the generated file above the library name
      # Note: docs are only included when include_library is true
      docs: |
        Automatically generate index / barrel / library files
        with all the export needed for your library.
      # You can define the name of the library to use within the index
      name: index_generator
      # Use package-style exports for internal files (e.g., 'package:app/main.dart' instead of 'main.dart')
      use_package_exports: true
      # Custom output path for the generated index file (optional)
      # If not specified, the file will be created in the directory_path
      output_path: lib/generated
  • directory_path: Path in which to create the index file. All filters will get the relative path from this directory
  • output_path: Custom output path for the generated index file. If not specified, the file will be created in the directory_path
  • file_name: Prioritize ownership in folders, otherwise it will use the one defined in the generator with default_file_name key. If it is missing, if the folder is lib it will use the package name otherwise the folder name
  • name: The name of the library used in the index dart file by the library keyword
  • include_library: Include or exclude the library keyword in the generated file. Defaults to true
  • docs: Documentation comments added above the library declaration. Only included when include_library is true
  • include | exclude: You can define filters that exclude or include files to be included in the index. The filters are passed paths relative to the index file. You can use Glob expressions.
  • exports: You can define specific export dart packages in index file. You can use package to export a dart file package or dart core library.
  • use_package_exports: When enabled, generates package-style exports for internal files (e.g., 'package:app/main.dart' instead of 'main.dart'). This is useful for libraries that want to ensure consistent import paths. Defaults to false.

Command

--help command output:


-s, --settings=<Define a yaml file path.> If not present use the "index_generator.yaml" file if it exists otherwise use the "pubspec.yaml" file.
-v, --[no-]verbose Print verbose logs
-h, --help

Build runner

index_generator can be run via build_runner. To do so, add the following dependancies:

dev_dependencies:
  build_runner: ^2.10.0
  index_generator: ^4.1.0

and enable it in build.yaml file

targets:
  $default:
    builders:
      index_generator:
        enabled: true

To generate use <dart|flutter> pub run build_runner <build|watch>

Features and bugs

Please file feature requests and bugs at the issue tracker.

Extra

  • mek_assets: Automatically generate dart classes / flutter pubspec entries for your assets files.
  • mek_data_class: Generate hashCode, ==, toString, copyWith and change methods with low code.

所属分类

依赖

依赖清单 11 项

源码仓库

使用此插件的项目