modular_codegen 3.0.4 copy "modular_codegen: ^3.0.4" to clipboard
modular_codegen: ^3.0.4 copied to clipboard

Code Generate for flutter_modular. Inject Automation. Annotation @Inject, @Param and @Data.

CI

modular_codegen #

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

Installation #

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 Module {
  @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 constructor 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.

24
likes
100
points
225
downloads

Publisher

unverified uploader

Weekly Downloads

Code Generate for flutter_modular. Inject Automation. Annotation @Inject, @Param and @Data.

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

MIT (license)

Dependencies

analyzer, build, build_resolvers, flutter, flutter_modular_annotations, glob, meta, source_gen

More

Packages that depend on modular_codegen