atomBase Project

Creating a Base Project in Flutter

This article is complete when combined with Clean Architecturearrow-up-right and Packagearrow-up-right.

Why Do You Need a Base Project?

Any Flutter developer, after working on multiple projects, wants to create a project template to speed up the initialization of future projects. However, extracting reusable parts and setting up the project structure from scratch can be time-consuming and tedious.

To solve this, I explored various ways to automate the creation of essential files and folders. One approach is using Windows command-line tools to generate files, but a more powerful tool that can save time for you and your team is Mason.

If you're unfamiliar with Mason, check out my guide: Boost Your Flutter Development Efficiency with Masonarrow-up-right

Essential Libraries for a Base Project

Below is a list of important libraries that I use to ensure the code is clean, scalable, and maintainable.

Dependency Injection with get_it

get_it on pub.devarrow-up-right

  • Helps with data retrieval and dependency injection.

  • Allows access to repositories anywhere in the project without manually passing data between classes.

Routing Management with go_router

go_router on pub.devarrow-up-right

  • Official Flutter routing libraryβ€”works well for both mobile and web.

  • Provides a clear and scalable way to manage navigation.

State Management with flutter_bloc

flutter_bloc on pub.devarrow-up-right

  • One of the most popular and efficient state management libraries.

  • If you're new to bloc, check out my guide:

rectangles-mixedBlocchevron-right

Code Generation with json_serializable & freezed

json_serializable on pub.devarrow-up-right freezed on pub.devarrow-up-right

  • Reduces repetitive boilerplate code.

  • Automatically generates helpful functions like copyWith, fromJson, and toJson for easier data handling.

Code Quality Improvement with lint

lint on pub.devarrow-up-right

  • Enforces Effective Dart coding standards.

  • Ensures consistent and readable code across teams.

  • If you haven’t read Effective Dart, check it out here: Effective Dartarrow-up-right.

App Icon Setup with flutter_launcher_icons

flutter_launcher_icons on pub.devarrow-up-right

  • Automatically generates app icons for both Android and iOS.

  • Saves time when setting up project branding.

Creating a Base Project with Mason

Below is how I structure the lib/ directory in my base project:

Outside of the lib/ folder, I also set up:

You can check out my base project template, which is built using Mason, here: dr_base_project on BrickHubarrow-up-right

Setting Up the Development Environment

A well-configured development environment helps prevent issues before the app reaches users. To easily switch between environments (dev, staging, production) without manual changes, I use an enum to store environment settings.

Defining the Environment Enum

You can extend this enum to include additional configurations like API keys, logging settings, and more.

If you want a full setup for Android and iOS flavors (dev, staging, production), check this guide. I use EnviEnum to control environment logic in Dart, and Flavor to control native app config and Firebase files.

slidersFlavorchevron-right

Initializing the App for Each Environment

Development Environment (Local Server)

Staging Environment (Internal Testing Before Release)

Production Environment (Final Release for Users)

Conclusion

Setting up a base project in Flutter saves a lot of time when starting a new project, making it easier to maintain and scale.

  • Use Mason to automatically generate necessary files and directories.

  • Leverage popular libraries such as go_router, flutter_bloc, json_serializable, and freezed to enhance development efficiency.

  • Configure dev, staging, and production environments to better manage application environments.

Buy Me a Coffeearrow-up-right | Support Me on Ko-fiarrow-up-right

Last updated