CI

modular_codegen

Code geneeration for flutter_modular. Injection automation. Annotations Injectable, Param and Data.

Instalation

Open your project's pubspec.yaml and add modular_codegen and build_runner as a dev dependency:

dev_dependencies:
  modular_codegen: any
  build_runner: any

Injection automation

Annotate your class with Injectable. Don't forget the part directive.

part 'home_controller.g.dart'; // ← part directive with your_file_name.g.dart

@Injectable() // ← Injectable annotation
class HomeController {
    ...

Execute the build_runner in the root of your project:

flutter pub run build_runner build

The generator will provide a $ClassName in the generated file, that can be injected in your module binds:

class HomeModule extends ChildModule {
  @override
  List<Bind> get binds => [
        $HomeController, // ← As the class name was `HomeController`, the generated injectable is `$HomeController`
      ];

Injection automation will rely on the class constructor's parameters to generate the bindings.

// home_controller.dart
part 'home_controller.g.dart'; // ← part directive with your_file_name.g.dart

@Injectable() // ← Injectable annotation
class HomeController {
    final ApiRepository repository;
    HomeController({
        this.repository, // ← The parameters of the constructur will define the generated binding
    })

    ...
}

// Generated home_controller.g.dart
Bind(i) => HomeController(
    repository: i.get<ApiRepository>(), // ← repository parameter from constructor
);

Injectable configuration

Injectable annotation has two optional boolean params: singleton and lazy. By default, they are set to true. Thus, you can easily disable singleton behavior and lazy-loading behavior by passing these arguments.

Example:

@Injectable(singleton: false) // ← Disables singleton behavior
class ProductController {
    ...

Route parameters and arguments (Navigator)

If you need to pass data to your controller through the Navigator, you may annotate your constructor's parameters with Param or Data.

Param for dynamic route

For example, if your route URL is going to have an id parameter, provide a String parameter with the same name and annotated with Param.

part 'product_controller.g.dart'; // ← part directive with your_file_name.g.dart

@Injectable() // ← Injectable annotation
class ProductController {
    final String id

    ProductController({@Param this.id}) // ← This annotation will allow you to pass the `id` parameter in the route URL, like `/product/:id`

    ...
}

Data for Navigator arguments

Similarly, if you are going to pass complex objects to your route, annotate your constructor's parameters with Data.

part 'product_controller.g.dart'; // // ← part directive with your_file_name.g.dart

@Injectable() // ← Injectable annotation
class ProductController {
    final ProductItem item

    ProductController({@Data this.item}) //<- add @Data annotation
    ...
}

Then, pass the argument parameter to Modular.to.pushNamed:

Modular.to.pushNamed('/product', arguments: ProductItem());

Features and bugs

Please send feature requests and bugs at the issue tracker.

Libraries

builder
modular_codegen