Cell


Cell is library of useful wrappers of values (cells) - pub.dev.

Cell - A wrapper around a mutable value.

OnceCell - A cell which can be written to only once.

LazyCell - A value which is initialized on the first access.

LazyCellAsync - A value which is asynchronously initialized on the first access.

Cell

A wrapper around a mutable value. Useful for mimicking references and wrapping primitives. Extensions exist for primitives. e.g. Cell<int> can be used similar to a normal int.

final cell = Cell<int>(10);
expect(cell.get(), 10);
cell.add(2);
expect(cell.get(), 12);
final anotherCell = Cell<int>(10);
final newCell = cell + anotherCell;
expect(newCell, 22);
expect(cell, 12);
expect(antherCell, 10);

The base type for all Cells is ConstCell.

OnceCell

A cell which can be written to only once. Similar to late final <variable>, but will never throw an error.

final cell = OnceCell<int>();
var result = cell.set(10);
expect(result, const Ok(()));
result = cell.set(20);
expect(result, const Err(20));

The base type for all OnceCells is NullableOnceCell.

LazyCell

A value which is initialized on the first access.

int callCount = 0;
final lazyCell = LazyCell<int>(() {
  callCount++;
  return 20;
});
final firstCall = lazyCell();
expect(callCount, equals(1));
expect(firstCall, equals(20));
final secondCall = lazyCell();
expect(callCount, equals(1));
expect(secondCall, equals(20));

The base type for all LazyCells is NullableLazyCell.

LazyCellAsync

A value which is asynchronously initialized on the first access.

int callCount = 0;
final lazyCell = LazyCellAsync<int>(() async {
  callCount++;
  return 20;
});
final firstCall = await lazyCell.force();
expect(callCount, equals(1));
expect(firstCall, equals(20));
final secondCall = lazyCell(); // Could also call `await lazyCell.force()` again.
expect(callCount, equals(1));
expect(secondCall, equals(20));

The base type for all LazyCellAsyncs is NullableLazyCellAsync.