Create data models easily, with no headache.
import 'package:data_fixture_dart/data_fixture_dart.dart';
class Company {
final String name;
final List
Company({this.name, this.employees}); }
extension CompanyFixture on Company { static _CompanyFixtureFactory factory() => _CompanyFixtureFactory(); }
class _CompanyFixtureFactory extends FixtureFactory
// If you need to override a model field, simply define a function that returns a FixtureDefinition
.
// To redefine the default definition, you must use the redefine
function.
FixtureDefinition
2. Then you can build the model by using its factory.
```dart
// Create a single object of type Company.
CompanyFixture.factory().makeSingle();
// Create a single object of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").make();
// Create 10 objects of type Company.
CompanyFixture.factory().makeMany(10);
// Create 10 objects of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").makeMany(10);
A factory can create a JSON Object from a generated model.
JSONFixtureFactory
protocol to the model factory.
import 'package:data_fixture_dart/data_fixture_dart.dart';
extension CompanyFixture on Company { static _CompanyFixtureFactory factory() => _CompanyFixtureFactory(); }
class _CompanyFixtureFactory extends JsonFixtureFactory
// This function define the json definition, using the default definition (function definition()
).
@override
JsonFixtureDefinition
// If you need to generate the JSON Object of an empty company, change the return type to JSONFixtureDefinition
// Previously the return was FixtureDefinition
.
JsonFixtureDefinition
2. Now you can generate the JSON Object of the model.
```dart
// Create a single JSON object of type Company.
CompanyFixture.factory().makeJsonObject();
// Create a single JSON object of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").makeJsonObject();
// Create a JSON Array of 10 objects of type Company.
CompanyFixture.factory().makeJsonArray(10)
// Create a JSON Array of 10 objects of type Company with no employees.
CompanyFixture.factory().empty("EmptyCompany").makeJsonArray(10);
// Create a Company object with its relative JSON object.
CompanyFixture.factory().makeSingleWithJsonObject();
// Create 10 Company object with its relative JSON objects.
CompanyFixture.factory().makeManyWithJsonArray(10);
JsonFixtureFactory
you can create a JSON from an external model object.
final company = CompanyFixture.factory.makeSingle();
final JSONObject = CompanyFixture.factory.makeJsonObjectFromSingle(from: company);
final companies = CompanyFixture.factory.makeMany(3); final JSONArray = CompanyFixture.factory.makeJsonArrayFromMany(from: companies);
### Using a custom Faker instance
Sometimes you want your `Faker` to have maybe a custom `seed` or custom `provider`.
In that case you can simply pass a custom `Faker` instance to either `define` or `redefine`
```dart
import 'package:data_fixture_dart/data_fixture_dart.dart';
extension NewsArticleFixture on NewsArticle {
static _NewsArticleFactory factory() => _NewsArticleFactory();
}
class _NewsArticleFixtureFactory extends FixtureFactory<NewsArticle> {
@override
FixtureDefinition<NewsArticle> definition() => define(
(Faker faker) => NewsArticle(
title: faker.lorem.sentence(),
content: faker.lorem.sentences(3).join(' '),
),
faker: Faker(
seed: Random().nextInt(1234567890),
provider: FakerDataProvider(
loremDataProvider: MyCustomLoremDataProvider(),
),
),
);
FixtureDefinition<Company> noContent() => redefine(
(newsArticle) => NewsArticle(
title: faker.lorem.sentence(),
content: null,
),
faker: Faker(
seed: Random().nextInt(9876543210),
provider: FakerDataProvider(
loremDataProvider: MyOtherCustomLoremDataProvider(),
),
),
);
}
data_fixture_dart is an open source project, so feel free to contribute. You can open an issue for problems or suggestions, and you can propose your own fixes by opening a pull request with the changes.
In order to test the package run this command
dart test