singleton_class

Annotation package to create a singleton style class. This package is for @SingletonClass() annotaion which indicates that a class is a singleton.

Breaking change

From v0.2.0+1, the whole usage is changed.

Usage

Install

Add this lines in your pubspec.yaml and run flutter pub get.

dependencies:
  singleton_class:

dev_dependencies:
  build_runner:
  singleton_class_generator:

Example

import 'package:singleton_class/singleton_class.dart';

part 'main.g.dart'; /// singleton_class_generator will automatically generate this.

// Simply annotate with a `@singleton`.
// Be careful that `s` is lowercase.
@singleton
class TestClass {
  // add this line to your class.
  // `_$TestClass` can be `_${CLASS_NAME}`
  static TestClass get instanc => _$TestClass;
  printer() {
    print(this.hashCode);
  }
}

// You can use your own constructor instead of the default constructor.
// Be careful that `S` is uppercase.
@Singleton(constructor: 'build')
class OtherConstructor {
  OtherConstructor.build();
}

main() {
  // You should access your class with `instance` getter.
  TestClass.instance.printer();
}

And then, run build_runner.

flutter pub run build_runner watch
# or
pub run build_runner watch

Libraries

singleton_annotation