Flutter: Speed up your workflow with a custom project creation script

For some reason, starting a new project always brings a small feeling of inherent joy and excitement. There is something about a clean slate that is just very comforting, the lack of constraints, the promise of the unknown!

However, one thing that can tend drain this joy a bit are the rote tasks involved in setting up a new project. This typically includes a number of steps:

  • remembering to properly configure the name and package of your project when running create
  • add your usual set of plugins to pubspec.yaml, one by one, either looking up versions on pub.dev, or using the flutter pub add cli
  • open main.dart and delete the starting boilerplate

While this is not much work at all (~5 minutes or less), it does get really boring after a few dozen times. One solution to this problem is to write a script that can take care of this stuff for us. So we went ahead and wrote one! It’s worth noting that, while we chose to use python, you could do this with nodejs, dart or any number of cli-based languages.

How does it work?

For the most part, the script just generates a massive command line string, and executes it. It then generates a new main.dart file at the end. We’ll walk through it below, or you could just read the code, it’s pretty self-explanatory.

First, create a new project with a proper name and package:

org = "com.example"
name = "app_name"
cmd = "flutter create --org " + org + " --project-name " + name + " .";

Next, add a series of flutter pub add commands, adding the latest versions of all default dependencies to your pubspec:

# dependencies
packages = [
 "cached_network_image",
 ... etc
];
for p in packages: 
	cmd += " && flutter pub add " + packages[packages.index(p)]

# dev-dependencies
packages = [
 "build_runner",
 ... etc
];

for p in packages : 
	cmd += " && flutter pub add --dev " + packages [packages .index(p)]

Then run the command which will create the project and add any dependencies:

os.system(cmd);

The final step is to replace the default main.dart with a minimalist version:

main = """import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Center(child: FlutterLogo()),
    );
  }
}"""

mainPath = "lib/main.dart"; 
os.remove(mainPath);
f = open(mainPath, "a")
f.write(main)
f.close()

Done!

Usage

Once the file is created, usage is simple:

  • copy the file into an empty folder
  • change the org and name values
  • run it with ./filename.py

After a few seconds you will have a clean project, fully set up and ready to go! No boilerplate to delete, and all dependencies are installed and set to the latest versions.

You will likely want to update the packages to the ones that you use the most, and tweak the main.dart to your style. This script is just one example of a set of defaults.

Note: On macOs and Linux, you will need to run chmod on the file in order to grant it execute permissions: chmod +x filename.py

Happy coding!


Need help building something cool in Flutter? We’d love to chat.

shawn.blais

Shawn has worked as programmer and product designer for over 20 years, shipping several games on Steam and Playstation and dozens of apps on mobile. He has extensive programming experience with Dart, C#, ActionScript, SQL, PHP and Javascript and a deep proficiency with motion graphics and UX design. Shawn is currently the Technical Director for gskinner.

@tree_fortress

2 Comments

  1. Stanley Muyuga August 26, 2023 at 2:58am

    Thanks for this nice piece of code, will be a great saver.

Leave a Reply

Your email address will not be published. Required fields are marked *