In this flutter tutorial, we will learn how to set up a folder structure in a flutter, where to put assets of our flutter project & how to create a flutter package that can be reused in any other flutter app.
Most of the time we start creating apps without even thinking about folder structures. But along the way, we start realizing that finding some file in the messy structure becomes hectic.
When building large Flutter apps, one of the first things we should decide is how to structure our project.
This ensures that the entire team can follow a clear convention and add features in a consistent manner.
Watch Youtube tutorial
Types of Folder Structures in Flutter
Flutter is a flexible platform, as it does not restrict its users to design folder in a specific way, whereas, it allow us to design a folder or project structure that suit our requirements, flexibility and need.
In flutter, we can adopt any folder structure we like and can also use MVC, MVVM combined with the folder structures defined below.
As there is no specific structure in flutter so in general, flutter community adopts 2 types of flutter structures:-
- Feature First
- Layer First
Lets talk about each of them in detail.
1. Layer based Flutter Structure
To keep things simple, suppose we have only two features in the app.
If we adopt the layer-first approach, our project structure may look like this:
2. Features based Flutter Structure
The feature-first approach demands that we create a new folder for every new feature that we add to our app. And inside that folder, we can add the layers themselves as sub-folders.
Using the same example as above, we would organize our project like this:
How to create a Custom Package in Flutter
While creating flutter apps, there are many features that need to be reused over and over again in other systems.
Specially, if you are working in a software house, or in some company where you don’t want to redo the development, we can create Packages in flutter.
Flutter packages can be created any where in the project structure but the recommended location to create a flutter package is under root folder or we can create folder called packages and in it we can create our packages.
Package that we will create in flutter app series will be auth_repository.
To create a package in flutter, follow the following steps:-
- Open terminal
- Terminal should point out to your location where you want to create your package
- Write the command flutter create –template package package_name
flutter create --template package auth_repository