inject_creator 0.0.1 copy "inject_creator: ^0.0.1" to clipboard
inject_creator: ^0.0.1 copied to clipboard

inject_creator is a convenient code generator for get_it.

inject_creator #

Pub Version

inject_creator is a convenient code generator for get_it.

Features #

  • Singleton
  • LazySingleton
  • Mutable
  • Alias
  • FactoryMethod
  • PostConstruct
  • DisposeMethod
  • Component

Getting started #

Install #

dependencies:
  get_it: ^6.0.0
  inject_creator: any

dev_dependencies:
  build_runner: ^2.0.0
  inject_creator_generator: any

Usage #

import 'main.dep.dart';

@EnableInjector(allReady: true)
// or @EnableInjector(entryPoint:'initDependencies', allReady: false)
void main() {
  configDependencies();
  // or initDependencies();
  runApp(const MyApp());
}

example:

import 'main.dep.dart';

// sync
getXxxx();

// async
await getSharedPreferences();

generate code:

flutter pub run build_runner build

Example #

Singleton #

@Singleton()
class LoginService {}


@Singleton(tag: 'customNameService')
class CustomNameService {}

abstract class AbstractService {}

@Singleton(tag: 'default', as: AbstractService)
class AbstractServiceImpl extends AbstractService {}

LazySingleton #

@LazySingleton()
class LazySingletonService {}

Mutable #

@Mutable()
class NoSingletonService {}

PostConstruct #

@Singleton()
class LoginService {
  @PostConstruct()
  void init() {
    print('PostConstruct init');
  }
}

FactoryMethod #

@LazySingleton(tag: 'asyncService')
class AsyncService {
  @FactoryMethod()
  static Future<AsyncService> withDependencies(LoginService loginService) {
    return Future.value(AsyncService());
  }
}

DisposeMethod #

@LazySingleton()
class DbService {
  @DisposeMethod()
  void dispose() {}
}

Alias #

@LazySingleton()
class FactoryMethodService {
  @FactoryMethod()
  static Future<FactoryMethodService> withDependencies(
      {@Alias('asyncService')
      required Future<AsyncService> asyncService,
      @Alias.by(SimpleAbstractService)
          required AbstractService abstractService}) {
    return Future(() => FactoryMethodService());
  }
}

Third party #

@Component()
class VendorComponent {

  @LazySingleton(tag: 'defaultSharedPreferences')
  Future<SharedPreferences> get sharedPreferences =>
      SharedPreferences.getInstance();

  @DisposeMethod()
  void dispose() {
    sharedPreferences.then((value) => value.clear());
  }
}
0
likes
140
pub points
0%
popularity

Publisher

verified publisherwkcdeie.eu.org

inject_creator is a convenient code generator for get_it.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on inject_creator