Factory<T extends Model> class abstract

The base Factory class for generating fake model data.

Factories provide a fluent API for creating models with fake data, similar to Laravel factories. Perfect for database seeding and testing.

Creating a Factory

class UserFactory extends Factory<User> {
  @override
  User newInstance() => User();

  @override
  Map<String, dynamic> definition() {
    return {
      'name': faker.person.name(),
      'email': faker.internet.email(),
      'is_active': true,
    };
  }
}

Using a Factory

// Create a single user
final user = await UserFactory().create();

// Create 50 users
final users = await UserFactory().count(50).create();

// Create with custom state
final admins = await UserFactory()
    .state({'role': 'admin'})
    .count(5)
    .create();

// Create without saving (in-memory only)
final mockUsers = UserFactory().count(10).make();

Constructors

Factory()

Properties

faker → Faker
Access the Faker instance for generating fake data.
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

count(int count) Factory<T>
Set the number of models to create.
create() Future<List<T>>
Create and persist models to the database.
definition() Map<String, dynamic>
Define the model's default state.
make() List<T>
Create models without persisting to the database.
newInstance() → T
Create a new instance of the model.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
state(Map<String, dynamic> state) Factory<T>
Apply a state override to the factory.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited