dynamic_constructor 1.0.0
dynamic_constructor: ^1.0.0 copied to clipboard
A flexible utility to dynamically instantiate classes and invoke constructors using positional and named parameters from dynamic data sources.
DynamicConstructor #
A package that allows you to dynamically instantiate classes by passing a constructor function and a set of parameters (List or Map). It automatically maps positional and named arguments to the constructor.
Features #
- Automatic Argument Mapping: Intelligently separates positional and named arguments from a single parameters object.
- Support for Lists and Maps: Provide parameters as a List (where the last element can be a Map for named arguments) or as a Map for purely named constructors.
- Type-Aware Parsing: Handles generic types in constructor signatures.
Getting started #
Add dynamic_constructor to your pubspec.yaml:
dependencies:
dynamic_constructor: ^1.0.0
Usage #
Check the /example folder for a complete Flutter demo.
Basic Example #
class User {
final String name;
final int age;
User(this.name, this.age);
}
final constructor = DynamicConstructor<User>(
User.new,
['John Doe', 25],
);
final User user = constructor.instance;
print(user.name); // John Doe
With Named Arguments #
class Product {
final String id;
final String title;
Product({required this.id, required this.title});
}
final constructor = DynamicConstructor<Product>(
Product.new,
{'id': 'P1', 'title': 'Smartphone'},
);
final Product product = constructor.instance;
Mixing Positional and Named #
class Post {
final int id;
final String content;
Post(this.id, {required this.content});
}
final constructor = DynamicConstructor<Post>(
Post.new,
[101, {'content': 'Hello World'}],
);
Additional information #
For more information, bugs or contributions, please visit the GitHub repository.