lazy_evaluation 1.1.0 copy "lazy_evaluation: ^1.1.0" to clipboard
lazy_evaluation: ^1.1.0 copied to clipboard

A simple package to cache and lazily evaluate objects that have a expensive initialization process.

Lazy Evaluation #

Build Status

A simple package to cache and lazily evaluate objects that have a costly initialization process.

Usage #

import 'package:lazy_evaluation/lazy_evaluation.dart';

main() {
  var myLazyString = Lazy<String>(() => 'this is lazily initialized');
  print(myLazyString.value); 
  print(myLazyString()); // you can use myLazyString() instead of myLazyString.value.
}




Note: With null-safety and the late keyword in dart now you basically have lazy initialization without needing this package like this:

main() {
  late var myLazyString = 'this is lazily initialized';
  print(myLazyString); 
}

It still makes sense to keep this up because it has some advantages over late lazies. for example with late you have to initialize the variable right where you're defining it or it wont be lazy.

Also this package has MutableLazy which gives you the ability to reset the variable's state by using the reset() function. after a reset() call the function reruns and updates the lazy value.

var number = 1;
var mutableLazy = MutableLazy(() => number * 3);
print(mutableLazy.value); // 3
number = 2;
print(mutableLazy.value); // 3
mutableLazy.reset();
print(mutableLazy.value); // 6

This package tries to have as little overhead as it can, but using the late keyword instead whenever it does the job is still recommended.

8
likes
120
pub points
71%
popularity

Publisher

unverified uploader

A simple package to cache and lazily evaluate objects that have a expensive initialization process.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on lazy_evaluation