factory 2.1.0 icon indicating copy to clipboard operation
factory: ^2.1.0 copied to clipboard

factory generator package. Use it to generate factories implementations.

Factory Generator #

Factory is created to help define easy to use model factories with predefined field assignment behaviour.

Factory title image

Post object creation example

Required boilerplate

Usage #

Mark target class with Factory annotation

class Item {
  final int left;
  final int right;

  const Item(this.left, this.right);

class ItemFactory extends _$ItemFactory with FakerProviderMixin {
  ItemFactory([FactoryContext? context, ContextKey key = defaultKey])
      : super(context, key);

void main() {
  Item item;

  // Create item with random [left] and [right] parameters
  // provided by [FakerProvider].
  item = ItemFactory()
      .create(); // creates Item(left: {random value}, right: {random value})

  // Provide explicit value of [left] parameter to constructor.
  item = ItemFactory().create(
    left: fromValue(10),
  ); // creates Item(left: 10, right: {random value})

  // Depend on [left] parameter when building [right] parameter.
  // For now order of variables is crucial.
  // First parameters in constructor initialized earlier
  // of any further parameters.
  // If we try to depend on [right] when instantiating [left] it will be null.
  item = ItemFactory().create(
    left: fromValue(10),
    right: (context, key) =>
        context.read<ItemReadonlyBuilder, Item>(key.up()).getLeft()! + 10,
  ); // creates Item(left: 10, right: 10 + 10)

  try {
    item = ItemFactory().create(
      left: (context, key) =>
          context.read<ItemReadonlyBuilder, Item>(key.up()).getRight()!,
      right: fromValue(10),
    ); // not creates item because left trying
    // to initialize before right initialized.
  } on Object {
    print('failed to create item');
pub points


unverified uploader

factory generator package. Use it to generate factories implementations.

Repository (GitHub)
View/report issues


API reference


Icon for licenses.MIT (LICENSE)


analyzer, build, build_config, code_builder, collection, dart_style, factory_annotation, source_gen


Packages that depend on factory