A Dart package that provides a Singleton
macro to transform classes into singletons automatically. This package leverages Dart's macro system to enforce the singleton pattern on the annotated classes.
- Automatic singleton implementation.
- Ensures a single instance of the class.
- Simplifies singleton pattern usage in Dart.
Add the following to your pubspec.yaml
file:
dependencies:
singleton_macro: ^0.0.2-dev.1
Annotate your class with @Singleton to make it a singleton:
// lib/main.dart
import 'package:singleton_macro/singleton_macro.dart';
@Singleton()
class MySingletonClass {
String name = "foo";
}
void main() {
final instance1 = MySingletonClass();
print(instance1.name); // Output: foo
final instance2 = MySingletonClass();
instance2.name = "bar";
print(instance1.name); // Output: bar
print(identical(instance1, instance2)); // Output: true
}
dart --enable-experiment=macros run lib/main.dart
Contributions are welcome! Please submit pull requests or create issues for any improvements or bugs you find.
If you find this project helpful, consider sponsoring it on GitHub:
Follow on YouTube For more tutorials and project walkthroughs, subscribe to our YouTube channel:
Maintained by Manish Gautam
Powered By Antinna