faker_gen 0.3.2
faker_gen: ^0.3.2 copied to clipboard
Code generator for fake factory functions. Generates fakeClassName() functions for classes annotated with @FakeIt().
faker_gen #
A code generator for creating fake data factories in Dart. Annotate your classes with @FakeIt() and get type-safe fake object builders with realistic data powered by faker.
Installation #
Add to your pubspec.yaml:
dependencies:
faker_annotation: ^0.3.0
dev_dependencies:
faker_gen: ^0.3.0
build_runner: ^2.4.0
Basic Usage #
import 'package:faker_annotation/faker_annotation.dart';
part 'user.g.dart';
@FakeIt()
class User {
final String name;
final int age;
final bool? active;
User({required this.name, required this.age, required this.active});
}
Run the generator:
dart run build_runner build
Use the generated factory:
// Create a fake user with random data
final user = fakeUser();
print(user.name); // Nasir Lesch
print(user.age); // 42
print(user.active); // true
// Override specific fields
final customUser = fakeUser(name: 'John', active: null);
print(customUser.name); // John
print(customUser.age); // 56
print(user.active); // null
// Generate multiple fake users
final users = fakeUser.many(10);
print(users.length); // 10
Annotations #
@FakeIt #
Marks a class for fake factory generation.
@FakeIt()
class Product {
final String id;
final double price;
Product({required this.id, required this.price});
}
@FakeAs #
Generates semantically meaningful fake data for fields.
@FakeIt()
class Person {
@FakeAs.uuid()
final String id;
@FakeAs.firstName()
final String name;
@FakeAs.email()
final String email;
@FakeAs.latitude()
final double lat;
@FakeAs.integer(min: 18, max: 65)
final int age;
@FakeAs.dateTime(minYear: 1990, maxYear: 2000)
final DateTime birthDate;
Person({...});
}
@FakeWith #
Use a custom function for complex/custom fake data:
List<String> fakeTags(Faker f) => f.nextListOf(() => f.nextWord(), maxLength: 5);
dynamic fakeData(Faker f) => f.nextInt();
@FakeIt()
class Article {
final String title;
@FakeWith(fakeTags)
final List<String> tags;
@FakeWith(fakeData)
final dynamic data;
Article({required this.title, required this.tags, this.data});
}
@FakeValue #
Use a constant value for a field:
@FakeIt()
class Config {
@FakeValue('production')
final String environment;
@FakeValue(42)
final int maxRetries;
Config({required this.environment, required this.maxRetries});
}
FakeGenerator #
Create reusable, parameterizable generators by extending FakeGenerator<T>. Use them directly as annotations:
class FakeAuthorGenerator extends FakeGenerator<Author> {
final String? defaultDomain;
const FakeAuthorGenerator({this.defaultDomain});
@override
Author generate(Faker faker) => Author(
name: faker.nextFullName(),
email: defaultDomain != null
? '${faker.nextUsername()}@$defaultDomain'
: faker.nextEmail(),
);
}
@FakeIt()
class Article {
final String title;
@FakeAuthorGenerator(defaultDomain: 'company.com')
final Author author;
Article({required this.title, required this.author});
}
Faker #
The Faker class provides all fake data methods. Use it in @FakeWith functions or standalone:
final f = Faker();
f.nextString(); // Random string
f.nextInt(min: 0, max: 100); // Random int in range
f.nextDouble(); // Random double 0.0-1.0
f.nextBool(); // Random boolean
f.nextDateTime(); // Random DateTime
f.nextEmail(); // Fake email
f.nextFirstName(); // Fake first name
// ...
Nested Classes #
Classes annotated with @FakeIt() are automatically composed:
@FakeIt()
class Address {
final String city;
Address({required this.city});
}
@FakeIt()
class Company {
final String name;
final Address address; // Automatically uses fakeAddress()
Company({required this.name, required this.address});
}
License #
MIT