.. | ||
doc | ||
example | ||
lib | ||
test | ||
.gitignore | ||
analysis_options.yaml | ||
CHANGELOG.md | ||
LICENSE.md | ||
pubspec.yaml | ||
README.md |
Platform Conditionable
A Dart implementation of Laravel's Conditionable trait, providing fluent conditional execution with method chaining.
Features
- Conditional method execution with
when
andunless
- Support for method chaining
- Cascade notation support with
whenThen
andunlessThen
- Fallback execution with
orElse
handlers - Support for both direct values and closure conditions
Usage
import 'package:platform_conditionable/platform_conditionable.dart';
// Add the Conditionable mixin to your class
class YourClass with Conditionable {
// Your class implementation
}
Basic Conditional Execution
class QueryBuilder with Conditionable {
final conditions = <String>[];
void addCondition(String condition) {
conditions.add(condition);
}
}
final query = QueryBuilder();
// Using when
query.when(hasStatus, (self, _) {
(self as QueryBuilder).addCondition("status = 'active'");
});
// Using unless
query.unless(category == null, (self, value) {
(self as QueryBuilder).addCondition("category = '$value'");
});
Method Chaining with Cascade Notation
class Config with Conditionable {
bool debugMode = false;
List<String> features = [];
}
final config = Config()
..whenThen(
isDevelopment,
() => config.features.add('debug-toolbar'),
)
..unlessThen(
isProduction,
() => config.features.add('detailed-logs'),
);
Using Fallback Handlers
final result = instance.when(
condition,
(self, value) => 'Primary result',
orElse: (self, value) => 'Fallback result',
);
Closure Conditions
instance.when(
() => someComplexCondition(),
(self, value) {
// Execute when condition is true
},
);
Features in Detail
The when
Method
Executes a callback if the condition is true:
instance.when(condition, (self, value) {
// Executed if condition is true
});
The unless
Method
Executes a callback if the condition is false:
instance.unless(condition, (self, value) {
// Executed if condition is false
});
Cascade Notation with whenThen
and unlessThen
For void operations that work well with cascade notation:
instance
..whenThen(condition1, () {
// Execute if condition1 is true
})
..unlessThen(condition2, () {
// Execute if condition2 is false
});
Fallback Handling
All methods support fallback execution through orElse
:
instance.when(
condition,
(self, value) => 'Primary action',
orElse: (self, value) => 'Fallback action',
);
instance.whenThen(
condition,
() => print('Primary action'),
orElse: () => print('Fallback action'),
);
Example
See the example for a complete demonstration of all features, including:
- Conditional query building
- Configuration setup
- Method chaining
- Fallback handlers
- Closure conditions
Important Notes
-
When using callbacks that need to access the instance methods, cast the
self
parameter to your class type:instance.when(condition, (self, value) { (self as YourClass).someMethod(); });
-
The
whenThen
andunlessThen
methods are designed for void operations and work well with cascade notation. -
Conditions can be either direct values or closures that return a value.
-
All methods return the instance by default if no callback is provided, enabling method chaining.