swagger_dart_generator 1.2.0
swagger_dart_generator: ^1.2.0 copied to clipboard
A Dart code generator that creates complete Data Layer a Package for Flutter project structure from Swagger/OpenAPI JSON files
𧬠Swagger Dart Generator #
You run the generator once and you instantly get a package you can import in any Flutter / Dart project to call your backend.
β¨ Features #
- Clean Architecture: Generates the data layer components (DataSources, Repositories, Models) for maintainability.
- Singleton Pattern: Creates a central (
<package_name>.dart) using lazy singletons access to all generated, You can Custom this just not use<package_name>.init(). - Model Generation: Creates Dart classes for API Request and Response models You can update only dtos With
mg_toolsfor gen Freezed DTOs from json. - Testing: Generates ready-to-use Integration Tests for all endpoints With Singletons default.
π» Installation #
Install the package globally using the Dart package manager. This makes the swagger_dart_generator command available anywhere on your system.
dart pub global activate swagger_dart_generator
Usage #
Run the command with the path to your swagger.json. The tool will generate a Dart package structure.
swagger_dart_generator
π Working with the Generated Package #
Import and use in your app:
import 'package:dio/dio.dart';
import 'package:example/example.dart';
import 'package:example/data/models/auth/requests/authlogin_req.dart';
void main() async {
final dio = Dio(BaseOptions(baseUrl: 'https://api.example.com'));
final api = Example.init(dio);
final loginResult = await api.repository.auth.authlogin(AuthloginReq());
loginResult.fold(
(failure) => print('β ${failure.message}'),
(success) => print('β
Logged in: $success'),
);
}
π§° Dependencies Generated #
dependencies:
dio:
dartz:
freezed_annotation:
json_annotation:
get_it:
logger:
dev_dependencies:
build_runner:
freezed:
json_serializable:
mg_tools:
test:
π Folder Structure Generated #
example
β£ .dart_tool
β β£ pub
β β β bin
β β β β mg_tools
β β β β β mg_tools.dart-3.9.2.snapshot
β β£ package_config.json
β β package_graph.json
β£ lib
β β£ data
β β β£ datasources
β β β β£ auth
β β β β β£ auth.dart
β β β β β auth_remote_datasource_impl.dart
β β β β£ product
β β β β β£ product.dart
β β β β β product_remote_datasource_impl.dart
β β β β user
β β β β β£ user.dart
β β β β β user_remote_datasource_impl.dart
β β β£ models
β β β β£ auth
β β β β β£ requests
β β β β β β£ auth_
β β β β β β£ authlogin_req.dart
β β β β β β£ authlogin_req.dto.json
β β β β β β£ authlogin_req.freezed.dart
β β β β β β authlogin_req.g.dart
β β β β β responses
β β β β β β£ authlogin_res.dart
β β β β β β£ authlogin_res.dto.json
β β β β β β£ authlogin_res.freezed.dart
β β β β β β authlogin_res.g.dart
β β β β£ product
β β β β β£ requests
β β β β β β£ products_req.dart
β β β β β β£ products_req.dto.json
β β β β β β£ products_req.freezed.dart
β β β β β β£ products_req.g.dart
β β β β β β£ productsid_req.dart
β β β β β β£ productsid_req.dto.json
β β β β β β£ productsid_req.freezed.dart
β β β β β β productsid_req.g.dart
β β β β β responses
β β β β β β£ products_res.dart
β β β β β β£ products_res.dto.json
β β β β β β£ products_res.freezed.dart
β β β β β β£ products_res.g.dart
β β β β β β£ productsid_res.dart
β β β β β β£ productsid_res.dto.json
β β β β β β£ productsid_res.freezed.dart
β β β β β β productsid_res.g.dart
β β β β user
β β β β β£ requests
β β β β β β£ useruser_id_req.dart
β β β β β β£ useruser_id_req.dto.json
β β β β β β£ useruser_id_req.freezed.dart
β β β β β β useruser_id_req.g.dart
β β β β β responses
β β β β β β£ useruser_id_res.dart
β β β β β β£ useruser_id_res.dto.json
β β β β β β£ useruser_id_res.freezed.dart
β β β β β β useruser_id_res.g.dart
β β β repositories
β β β β£ auth
β β β β β£ auth.dart
β β β β β auth_repository_impl.dart
β β β β£ product
β β β β β£ product.dart
β β β β β product_repository_impl.dart
β β β β user
β β β β β£ user.dart
β β β β β user_repository_impl.dart
β β£ end_points.dart
β β£ example.dart
β β failure.dart
β£ test
β β£ auth_test.dart
β β£ product_test.dart
β β user_test.dart
β£ .gitignore
β£ CHANGELOG.md
β£ README.md
β£ analysis_options.yaml
β£ pubspec.lock
β£ pubspec.yaml
β swagger.json
π€ Contributing #
Pull requests, issues and featureβrequests are welcome!
If you find bugs or want to support new features (e.g., custom headers, caching, plugin support) open an issue.
π License #
MIT Β© 2025 Mohamad Alghanem
