Flutter Lecture 8
File and Package Management
System in Flutter
Introduction:
Effective file management strategies in Dart, focusing on "Layer First" and "Feature First" architectures, as
well as exploring package management. This lecture aims to provide a comprehensive understanding of
structuring code and managing dependencies in Flutter.
File Management Strategies:
1.Layer First Architecture:
1. Explanation: Layering organizes code based on architectural layers (e.g., presentation, data, domain)
within the project.
lib/
|-- presentation/
Example Structure: | |-- screens/
| |-- widgets/
|-- data/
| |-- models/
| |-- repositories/
|-- domain/
| |-- entities/
| |-- use_cases/
5 Search:
Feature First Architecture: • Search functionality for various sections of the app
Explanation: 6 Messaging:
Organizes code based on individual features or modules of • Chat feature
• Inbox
the application. 7 Notifications:
Example : • Push notifications handling
1 Authentication: • In-app notifications
• Login 8 Shopping:
• Sign-up • Product listing
• Password reset • Cart management
• Social login (Google, Facebook, etc.) • Checkout process
2 User Profile: 9 Payment:
• User details • Payment gateway integration
• Profile settings • Transaction history
• Avatar management 10. Media:
3 Home: • Image gallery
• Main dashboard or landing screen • Video player
• Widgets for displaying different content • Audio player
4 Settings: 11 Maps and Location:
• Map display
• App settings
• Geolocation services
• Notification preferences
• Theme preferences
Example structure:
--lib
|--feature_1
| |--screens
| |--widgets
| |--models
| |--services
| |--view_models
|--feature_2
| |--screens
| |--widgets
| |--models
| |--services
| |--view_models
| ...
Folder by Feature CLI Utility
To help with the folder by feature pattern, I authored a very
opinionated CLI tool that generates the folder structure and the feature1/
required files within them to provide an excellent structure for code ┣ domain/
organization, testing while still being maintainable and easy to use. ┃ ┣ models/
┃ ┃ ┗ feature1_model.dart
┃ ┣ repository/
┃ ┃ ┗ feature1_repository.dart
┃ ┣ services/
┃ ┃ ┗ feature1_service.dart
┃ ┗ feature1_domain.dart
It generates a folder structure like below: ┣ providers/
┃ ┣ feature1_provider.dart
┃ ┗ providers.dart
┣ screens/
┃ ┣ feature1_screen.dart
┃ ┗ screens.dart
┣ widgets/
┃ ┣ feature1_widget.dart
┃ ┗ widgets.dart
┗ index.dart
Command Usage :wrench:
Make sure you are in the root of your project
ff generate -n <feature_name> -t <type>
-p <path>
The different components in the folder structure are:
•Domain → Models — contains all the data models and JSON to/from Dart helper
functions
•Domain → Repository — contains abstract classes that describe the feature functionality
•Domain → Services — contains the actual implementation of the repository
•Providers — contains everything related to the state for that particular feature
•Screens — contains full screens that have a Scaffold
•Widgets — contains all the widgets required for that particular feature
Each of the folders has a common export file that simply exports everything which
enables us to tidy up our imports when importing multiple things from the same feature.
Package Management:
Creating a Dart Package:
1.Using Flutter Command:
•The flutter create --template=package my_package command initializes a new Dart package named
my_package using Flutter's package template.
•This command generates the necessary files and folder structure for a Dart package.
2.Directory Structure:
•Upon execution, the command creates a directory named my_package containing the package's files
and configurations.
•Inside my_package, essential files include:AX
•lib/: Directory containing Dart code.
•pubspec.yaml: Configuration file defining the package metadata and dependencies.
3.Package Metadata:
•The pubspec.yaml file contains crucial information about the package:
Utilizing the Package:
To use this package in other Dart
projects, define the package as a
dependency in the project's
pubspec.yaml file:
dependencies:
my_package:
path: ../my_package // Replace with the actual path to the package
Import, Export, Part, and Library Keywords:
1.Import and Export:
1. Import: Brings code from one file or package into another file.
2. Export: Exposes code from the current file or package for use in other files.
2.Part and Part of:
1. Part: Indicates a file that is part of a particular library.
2. Part of: Specifies that a file is part of a specific library.
3.Library Keyword:
1. Used to define the current file as a library with a specific name.
Example:
File: my_library.dart File: utils.dart
library my_library;
part of my_library;
part 'utils.dart'; // Includes another part of the library
// Code related to utility functions or classes
// Code elements