🧅 Onion Structure in Flutter

·

2 min read

Table of contents

No heading

No headings in the article.

Hi, This is Sagar Sharma I'm a Full Stack Developer.

|-- core/
|   |-- entities/
|   |   |-- user.dart            # User entity class
|   |-- repositories/
|   |   |-- user_repository.dart # UserRepository interface
|   |-- usecases/
|       |-- user_usecase.dart    # User use cases (CRUD operations)
|
|-- data/
|   |-- datasources/
|   |   |-- user_datasource.dart # User data source interface (API, database, etc.)
|   |-- repositories/
|       |-- user_repository_impl.dart # UserRepository implementation
|
|-- domain/
|   |-- repositories/
|   |   |-- user_repository.dart # UserRepository interface
|   |-- usecases/
|       |-- user_usecase.dart    # User use cases interface
|
|-- presentation/
|   |-- pages/
|   |   |-- user_list_page.dart   # UI page for listing users
|   |   |-- user_detail_page.dart # UI page for user details
|   |-- widgets/
|       |-- user_list_item.dart   # UI widget for displaying a user in a list
|       |-- user_form.dart        # UI widget for user CRUD operations
|
|-- app.dart                       # Main entry point of the application

Now let's go through each folder and provide some sample code snippets:

  1. Entities (core/entities/user.dart):

     class User {
       final String id;
       final String name;
       final int age;
    
       User({required this.id, required this.name, required this.age});
     }
    
  2. Repositories (core/repositories/user_repository.dart):

import '../entities/user.dart';

abstract class UserRepository {
  Future<List<User>> getUsers();
  Future<User> getUserById(String id);
  Future<void> addUser(User user);
  Future<void> updateUser(User user);
  Future<void> deleteUser(String id);
}
  1. Use Cases (core/usecases/user_usecase.dart):
import '../repositories/user_repository.dart';
import '../entities/user.dart';

class UserUseCase {
  final UserRepository userRepository;

  UserUseCase({required this.userRepository});

  Future<List<User>> getUsers() async {
    return userRepository.getUsers();
  }

  // Implement other CRUD operations similarly
}
  1. Data Sources (data/datasources/user_datasource.dart):
import 'package:your_package_name/core/entities/user.dart';

abstract class UserDataSource {
  Future<List<User>> getUsers();
  Future<User> getUserById(String id);
  Future<void> addUser(User user);
  Future<void> updateUser(User user);
  Future<void> deleteUser(String id);
}
  1. Repositories Implementation (data/repositories/user_repository_impl.dart):
import 'package:your_package_name/core/repositories/user_repository.dart';
import 'package:your_package_name/data/datasources/user_datasource.dart';

class UserRepositoryImpl implements UserRepository {
  final UserDataSource userDataSource;

  UserRepositoryImpl({required this.userDataSource});

  @override
  Future<List<User>> getUsers() async {
    return userDataSource.getUsers();
  }

  // Implement other CRUD operations similarly
}
  1. User Interface (presentation/pages/user_list_page.dart, presentation/pages/user_detail_page.dart, etc.):

    Sample UI code for user list page, user detail page, etc.

This is a basic structure to get started with. You can expand and refactor it as per your project requirements and scale. Remember to replace "your_package_name" with your actual package name in the code.

Thank you Follow me for more..

Did you find this article valuable?

Support Sagar Sharma's Blog by becoming a sponsor. Any amount is appreciated!

Â