factory 1.0.1 copy "factory: ^1.0.1" to clipboard
factory: ^1.0.1 copied to clipboard

outdated

factory generator package. Use it to generate factories implementations.

example/main.dart

// ignore_for_file: unused_local_variable

import 'package:factory_annotation/factory_annotation.dart';

part 'main.factory.dart';

class Item {
  final int left;
  final int right;

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

@Factory(Item)
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');
  }
}
1
likes
0
pub points
28%
popularity

Publisher

unverified uploader

factory generator package. Use it to generate factories implementations.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

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

More

Packages that depend on factory