initialize 0.6.2+7

  • Installing
  • Versions
  • --

Initialize Build Status

This package provides a common interface for initialization annotations on top level methods, classes, and libraries. The interface looks like this:

abstract class Initializer<T> {
  dynamic initialize(T target);

The initialize method will be called once for each annotation. The type T is determined by what was annotated. For libraries it will be a LibraryIdentifier representing that library, for a class it will be the Type representing that class, and for a top level method it will be the Function object representing that method.

If a future is returned from the initialize method, it will wait until the future completes before running the next initializer.



There is one initializer which comes with this package, @initMethod. Annotate any top level function with this and it will be invoked automatically. For example, the program below will print hello:

import 'package:initialize/initialize.dart';

printHello() => print('hello');

main() => run();

Running the initializers

In order to run all the initializers, you need to import package:initialize/initialize.dart and invoke the run method. This should typically be the first thing to happen in your main. That method returns a Future, so you should put the remainder of your program inside the chained then call.

import 'package:initialize/initialize.dart';

main() {
  run().then((_) {
    print('hello world!');


During development a mirror based system is used to find and run the initializers, but for deployment there is a transformer which can replace that with a static list of initializers to be ran.

This will create a new entry point which bootstraps your existing app, this will have the same file name except .dart with be replaced with .initialize.dart. If you supply an html file to entry_points then it will bootstrap the dart script tag on that page and replace the src attribute to with the new bootstrap file.

Below is an example pubspec with the transformer:

name: my_app
  initialize: any
- initialize:
    entry_points: web/index.html

Creating your own initializer

Lets look at a slightly simplified version of the @initMethod class:

class InitMethod implements Initializer<Function> {
  const InitMethod();

  initialize(Function method) => method();

You would now be able to add @InitMethod() in front of any function and it will be automatically invoked when the user calls run().

For classes which are stateless, you can usually just have a single const instance, and that is how the actual InitMethod implementation works. Simply add something like the following:

const initMethod = const InitMethod();

Now when people use the annotation, it just looks like @initMethod without any parenthesis, and its a bit more efficient since there is a single instance. You can also make your class private to force users into using the static instance.

Creating custom transformer plugins

It is possible to create a custom plugin for the initialize transformer which allows you to have full control over what happens to your annotations at compile time. Implement InitializerPlugin class and pass that in to the InitializeTransformer to make it take effect.

You will need to be familiar with the analyzer package in order to write these plugins, but they can be extremely powerful. See the DefaultInitializerPlugin in lib/build/initializer_plugin.dart as a reference. Chances are you may want to extend that class in order to get a lot of the default functionality.


  • Strong mode fixes.



  • Update analyzer minimum version to 0.27.2.


  • Stop using deprecated analyzer apis.


  • Update code_transformers, analyzer, and html version constraints.


  • Update transformer_test dep to 0.2.x.


  • Add support for code_transformers 0.4.x.


  • Update analyzer to >=0.27.0 <0.28.0.


  • Update analyzer to <0.27.0 and dart_style to <0.3.0.


  • Update to work with deferred loaded libararies in reflective mode, (but not yet in the transformer).


  • Update to analyzer <0.26.0.


  • Fix bootstrap files to return the result of the original main.


  • Switch html5lib package dependency to html.


  • Make sure to always use the canonical libraries and super declarations in development mode. This eliminates an uncommon issue where a single initializer could be ran more than once.


  • Private identifiers will now be evaluated and inlined into the bootstrap file by the transformer, 29.


  • Fix for LibraryIdentifier paths when initializer starts from inline scripts inside of subfolders.


  • Added the from option to run. This should be a Uri pointing to a library in the mirror system, and throws if not in mirrors mode. This can be used to run initializers in a custom order at development time.
  • This package no longer tries to handle initializing scripts found in html imports. If you need this feature please use initWebComponents from the web_components package.


  • Make sure to crawl the entire supertype chain for annotations, and run them in reverse order.


  • Change to source order-crawling of directives instead of alphabetical. The one exception is for part directives, those are still crawled in alphabetical order since we can't currently get the original source order from the mirror system.


  • Fix some analyzer warnings.


  • Fix an issue where for certain programs the transformer could fail, 33.


  • Update to use mock dart sdk from code_transformers and update the analyzer and code_transformers dependencies.


  • Fix up mirror based import crawling so it detects dartium and only crawl all libraries in the mirror system in that case.


  • Fix handling of exported libraries. Specifically, annotations on exported libraries will now be reached and imports in exported libraries will now be reached.
  • Add support for scripts living in html imports without adding an html dependency by crawling all libraries in the mirror system in reverse order, instead of just the root one.


  • Make sure to always use path.url in the transformer.


  • Added support for more types of expressions in constructor annotations. More specifically, any const expressions that evaluate to a String, int, double, or bool are now allowed. The evaluated value is what will be inlined in the bootstrap file in this case.


  • The InitializePluginTransformer is gone in favor of a new InitializerPlugin class which you can pass a list of to the InitializeTransformer. These plugins now have access to the fully resolved ast nodes and can directly control what is output in the bootstrap file.


Lots of transformer updates:

  • The new_entry_point option is gone. The bootstrapped file will now always just be the original name but .dart will be replaced with .initialize.dart.
  • The html_entry_point option is gone, and the file extension is now used to detect if it is an html or dart file. You should no longer list the dart file contained in the html file. Effectively resolves 13.
  • The entry_point option has been renamed entry_points and now accepts either a single file path or list of file paths. Additionally, it now supports Glob syntax so many files can be selected at once. Resolves 19.


  • Added InitializePluginTransformer class in plugin_transformer.dart which provides a base transformer class which can be extended to perform custom transformations for annotations. These transformers should be included after the main initialize transformer and work by parsing the bootstrap file so the program doesn't need to be re-analyzed.


  • Library initializers now pass a LibraryIdentifier to initialize instead of just a Symbol. This provides the package and path to the library in addition to the symbol so that paths can be normalized.


  • entryPoint and newEntryPoint transformer options were renamed to entry_point and new_entry_pont.

  • Added html_entry_point option to the transformer. This will search that file for any script tag whose src is entry_point and rewrite it to point at the bootstrapped file new_entry_point.

  • Top level properties and static class properties are now supported in initializer constructors, as well as List and Map literals, 5.


Quick fix for the transformer on windows.


Initial beta release. There is one notable missing feature in the release regarding constructor arguments, see 5.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  initialize: ^0.6.2+7

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:initialize/initialize.dart';
Version Uploaded Documentation Archive
0.6.2+7 Aug 30, 2017 Go to the documentation of initialize 0.6.2+7 Download initialize 0.6.2+7 archive
0.6.2+6 May 22, 2017 Go to the documentation of initialize 0.6.2+6 Download initialize 0.6.2+6 archive
0.6.2+5 Nov 16, 2016 Go to the documentation of initialize 0.6.2+5 Download initialize 0.6.2+5 archive
0.6.2+4 Nov 15, 2016 Go to the documentation of initialize 0.6.2+4 Download initialize 0.6.2+4 archive
0.6.2+3 Nov 14, 2016 Go to the documentation of initialize 0.6.2+3 Download initialize 0.6.2+3 archive
0.6.2+2 Mar 17, 2016 Go to the documentation of initialize 0.6.2+2 Download initialize 0.6.2+2 archive
0.6.2+1 Jan 20, 2016 Go to the documentation of initialize 0.6.2+1 Download initialize 0.6.2+1 archive
0.6.2 Dec 10, 2015 Go to the documentation of initialize 0.6.2 Download initialize 0.6.2 archive
0.6.1+2 Sep 2, 2015 Go to the documentation of initialize 0.6.1+2 Download initialize 0.6.1+2 archive
0.6.1 Apr 30, 2015 Go to the documentation of initialize 0.6.1 Download initialize 0.6.1 archive

All 33 versions...

Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

This package is not analyzed, because it is discontinued.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.9.0-dev.7.1 <2.0.0