0 purchases
tdd data builder
Data Builder for TDD Clean Code Architecture #
We always generate dto and service files while using TDD architecture.
This package aids you to generate this files
Usage/Examples #
This package uses retrofit and dio for api usage.
Create DTO file #
dart run tdd_data_builder dto <DTO-NAME>
copied to clipboard
Replace
You may create entity for your DTO by adding --entity
dart run tdd_data_builder dto <DTO-NAME> --entity
copied to clipboard
Create Service file #
dart run tdd_data_builder service <SERVICE-NAME>
copied to clipboard
Replace
Create DataResponse class file #
Many back-end services returns response inside data property of their JSON file as follow:
{
"data" : {
...
}
}
copied to clipboard
We created a special DataResponse class to extract the data from the this JSON. It means you don't need to create class which holds data property every time. Run this code:
dart run tdd_data_builder data-response
copied to clipboard
It generates the file in the following path:
lib/data/dto/default/default_response.dart
If there is a need to add a new route from api which contains data, you just register your dto class inside data-response. That's it.
part 'data_response.g.dart';
@JsonSerializable()
class DataResponse<T> {
@JsonKey(name: "data")
@_Converter()
final List<T> data;
const DataResponse({
required this.data,
});
factory DataResponse.fromJson(Map<String, dynamic> json) =>
_\$DataResponseFromJson<T>(json);
Map<String, dynamic> toJson() => _\$DataResponseToJson(this);
}
class _Converter<T> implements JsonConverter<T, Object?> {
const _Converter();
@override
T fromJson(Object? json) {
if (json is Map<String, dynamic>) {
switch (T) {
//case SampleDto:
//return SampleDto.fromJson(json) as T;
}
return DefaultResponse<T>.fromJson(json) as T;
}
return json as T;
}
@override
Object? toJson(T object) => object;
}
copied to clipboard
Here commented SampleDTO is your DTO which comes as a value of data property in JSON.
⚠ MUST do everytime #
When you create a dto or service file, it just generate the template. You fill your class with necessary attributes and methods as your needs. After all, you must run build_runner package to generate the part files.
flutter pub run build_runner build
copied to clipboard
🔗 Links #
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.