This project demonstrates how to make REST API calls in a Flutter application using the Retrofit package for cleaner and more maintainable code.
- Introduction
- Features
- Getting Started
- Setup and Installation
- Project Structure
- Usage
- Contributing
- License
This project aims to simplify making REST API calls in a Flutter application by leveraging the Retrofit package. Retrofit helps in handling dynamic headers, parameters, requests, and responses in a custom and secure manner.
- Clean and maintainable code using Retrofit.
- JSON serialization and deserialization.
- Asynchronous data fetching with FutureBuilder.
- Simple UI to display data fetched from API.
To get started with this project, you need to have Flutter installed on your local machine. Follow the official guide to set up Flutter.
-
Clone the Repository:
git clone https://github.com/yourusername/flutter-retrofit-api.git cd flutter-retrofit-api
-
Install Dependencies:
flutter pub get
-
Run the Project:
flutter run
flutter-retrofit-api/
│
├── lib/
│ ├── main.dart
│ ├── services/
│ │ ├── api_service.dart
│ ├── models/
│ │ ├── post_model.dart
│ ├── pages/
│ │ ├── home_page.dart
│
├── pubspec.yaml
main.dart
: Entry point of the Flutter application.services/api_service.dart
: Contains the abstract class for Retrofit API calls.models/post_model.dart
: Defines the model class and JSON serialization logic.pages/home_page.dart
: Contains the UI implementation to display the fetched data.
Add the required dependencies in pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
retrofit: ^2.0.0
dio: ^4.0.0
json_serializable: ^6.0.0
dev_dependencies:
build_runner: ^2.1.0
json_serializable: ^6.0.0
Create an abstract class in lib/services/api_service.dart
:
Define the model class in lib/models/post_model.dart
:
Implement the UI to display the fetched posts in lib/pages/home_page.dart
:
Contributions are welcome! Please create a pull request with a description of your changes.
This project is licensed under the MIT License - see the LICENSE file for details.
Enjoy coding! If you have any questions, feel free to open an issue or reach out to the community.