Conditional constructor

const Conditional(
  1. List<ClassType<Condition>> conditions
)

The Conditional annotation in Jetleaf allows developers to specify conditions that must be satisfied for a class or method to be processed by the framework.

This annotation is typically used on configuration classes, pods, or methods that should only be included when certain runtime conditions are met. Conditions are implemented as classes that implement the Condition interface.

Key Features:

  • Supports multiple conditions; all must match for the annotated type to be processed.
  • Can be applied to both classes and methods.

Usage Example:

import 'package:jetleaf/jetleaf.dart';

// A condition that only matches when the application is running in production
class OnProductionEnvironmentCondition implements Condition {
  @override
  bool matches(ConditionalContext context, ClassType<Object> classType) {
    return context.environment.activeProfiles.contains('production');
  }
}

// Apply conditional configuration
@Conditional([ClassType<OnProductionEnvironmentCondition>()])
class ProductionDataSourceConfig {
  // Beans/pods defined here will only be registered in production
}

In this example, ProductionDataSourceConfig will only be processed by Jetleaf if OnProductionEnvironmentCondition evaluates to true. This allows developers to define environment-specific configuration easily.

Implementation

const Conditional(this.conditions);