Modules
Modules are the core organizational unit in Air Framework. Every feature in your application should reside within a module.
Module Structure
Section titled “Module Structure”A standard module generated by the CLI follows this structure:
lib/modules/products/├── products_module.dart # Module definition├── ui/│ ├── views/ # UI screens│ └── state/ # State management├── services/ # Business logic├── repositories/ # Data layer└── models/ # Data modelsThe AppModule API
Section titled “The AppModule API”To create a module, extend AppModule. This class provides several hooks to configure your module.
class MyModule extends AppModule { @override String get id => 'my_module';
@override String get name => 'My Feature'; // Optional
@override String get version => '1.0.0'; // Optional, supports semver
@override List<AirRoute> get routes => [...];
@override List<String> get dependencies => ['auth:^1.0.0']; // Explicitly required modules
@override void onBind(AirDI di) { // Register your services here (Sync ONLY) }
@override Future<void> onInit(AirDI di) async { // Perform async initialization (DB, API checks) }
@override Future<void> onDispose(AirDI di) async { // Cleanup resources }}Lifecycle
Section titled “Lifecycle”Modules go through a specific lifecycle managed by the ModuleManager:
-
Binding (
onBind): The framework gathers all service registrations. This must be strictly synchronous. -
Initialization (
onInit): The framework calls this asynchronously. Use this for heavy lifting like initializing local storage, databases, or triggering initial state pulses.@overrideFuture<void> onInit(AirDI di) async {// Initialize storageawait di.get<SettingsService>().init();// Must trigger initial state pulses for your statedi.get<NotesState>();} -
Ready: The module is fully functional and its routes are available.
-
Disposal (
onDispose): Triggered when a module is unregistered.
Dependency Resolution
Section titled “Dependency Resolution”Air Framework supports semantic versioning for module dependencies. If a required module is missing, the app will throw a StateError. If there is a version mismatch, a warning will be logged to the console in Yellow.