zef_helpers_lazy 2.0.0 copy "zef_helpers_lazy: ^2.0.0" to clipboard
zef_helpers_lazy: ^2.0.0 copied to clipboard

A Dart package providing a versatile implementation of lazy initialization.

example/zef_helpers_lazy_example.dart

import 'package:zef_helpers_lazy/zef_helpers_lazy.dart';

class Config {
  final String data;

  Config(this.data);

  void display() {
    print("Config data: $data");
  }
}

void main() async {
  Lazy<Config> lazyConfig = Lazy<Config>(
    factory: () => Config("Initial data loaded"),
    onCreate: (config) => print("Config created with data: ${config.data}"),
    shouldExpire: (config) => config.data == "Expired data",
    onDestroy: (config) =>
        print("Config with data '${config.data}' is being destroyed"),
    expiryDuration: Duration(milliseconds: 10), // Example expiry duration
  );

  // Access the lazy instance to trigger creation
  print("Accessing lazyConfig for the first time:");
  final accessOne = await lazyConfig.value;
  accessOne.display();

  // Access it again to show that it doesn't recreate the instance
  print("\nAccessing lazyConfig again:");
  final accessTwo = await lazyConfig.value;
  accessTwo.display();

  // Simulate expiry by waiting longer than the expiry duration
  Future.delayed(Duration(milliseconds: 20), () async {
    print("\nAccessing lazyConfig after expiry:");
    final accessThree = await lazyConfig.value;
    accessThree.display(); // This should recreate the instance
  });

  // Resetting the lazy instance manually
  print("\nResetting lazyConfig:");
  lazyConfig.reset();

  // Accessing after reset to show it recreates the instance
  print("\nAccessing lazyConfig after reset:");
  final accessFour = await lazyConfig.value;
  accessFour.display();
}
1
likes
160
points
171
downloads

Publisher

verified publisherzooper.dev

Weekly Downloads

A Dart package providing a versatile implementation of lazy initialization.

Homepage
Repository (GitHub)
View/report issues

Topics

#lazy

Documentation

API reference

Funding

Consider supporting this project:

buymeacoffee.com

License

MIT (license)

More

Packages that depend on zef_helpers_lazy