modular_codegen

Last updated:

0 purchases

modular_codegen Image
modular_codegen Images
Add to Cart

Description:

modular codegen

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
copied to clipboard
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 {
...
copied to clipboard
Execute the build_runner in the root of your project:
flutter pub run build_runner build
copied to clipboard
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`
];
copied to clipboard
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
);
copied to clipboard
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 {
...
copied to clipboard
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`

...
}
copied to clipboard
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
...
}
copied to clipboard
Then, pass the argument parameter to Modular.to.pushNamed:
Modular.to.pushNamed('/product', arguments: ProductItem());
copied to clipboard
Features and bugs #
Please send feature requests and bugs at the issue tracker.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.