initializer 0.3.1 initializer: ^0.3.1 copied to clipboard
Automatically generate code for aggregate initialization code by annotating Dart variable/function/field/method.
This library is just a simple library for integrating initialization code.
Annotate code that needs to be initialized with @Initializer()
to generate the aggregate function initializer()
.
Call initializer()
at the beginning of your program (such as the beginning of main
function), then the relevant code will be initialized;
Features #
supported types:
- global variable
- global function without required parameters
- static fields of a class
- static methods of a class without required parameters
Getting started #
- Add the following to your project's pubspec.yaml and run pub get.
dependencies:
initializer_annotation: ^0.1.0
dev_dependencies:
initializer: ^0.1.0
- Import initializer_annotation.dart from a file in your project.
import 'package:initializer_annotation/initializer_annotation.dart';
- Annotate your code with classes defined in package:initializer_annotation.
-
See example/lib/example.dart for an example of a file using these annotations.
-
See example/lib/init.init.dart for the generated file.
- Run
dart run build_runner build
to generate files into your source directory.
NOTE: If you're using Flutter, replace pub run with flutter pub run.
Usage #
-
you can config the
output file path
/group order
in build.yamltargets: $default: builders: initializer: options: output_path: lib/init.init.dart order: - default - group1 - group2
-
use global variable/function(without required parameters)
@Initializer() final initVar = init(); bool _inited; @Initializer() bool init() => _inited = true; late final bool _inited2; @Initializer(group: 'group2') bool init2() => _inited2 = true;
-
use static fields/methods(without required parameters) of a class NOTE:Need annotate
class name
andfield
/method
both.@Initializer() class InitA { @Initializer() static final initVar = init(); static late final int _init; @Initializer() static int init() { _init = 1; return _init; } }