Create almost empty Flutter project with "hello world" App

Flutter - Create simple almost empty hello world project app

By default, when creating a new Flutter app project with the flutter create command, a counter app project will get generated. Instead of deleting the unwanted pieces of code every time I create a new project I wondered if there was a way to create an even more basic, more empty project. First, we are going to create a simpler version of the Counter App project and from there transform it into a generic "hello world" app.

First, let's quickly examine the default Flutter project.

Default Flutter project

By default, the "flutter create" command will create a counter app project as shown below:

Flutter default counter app after using 'flutter create' command.

If we look into /lib/main.dart file, excluding comments it will be almost 70 lines long. It includes one Stateless and one Stateful widget.

A simpler version of counter app using a --sample argument

We can use the "flutter create" command that uses --sample argument to create a bit simpler counter app. It is based on a Scaffold widget sample. It will also have one Stateless and one Stateful widget and have the same functionality as a default Counter App.

We create it by using the following command:

flutter create simpler_counter_app --sample=material.Scaffold.1
Note: The material.Scaffold.1 sample is just one of almost 500 other samples we can choose from while creating an app project.

And the resulting app will look like this:

Flutter simpler counter app project using Scaffold sample

The code inside the /lib/main.dart file (excluding comments) will contain only about 40 lines of code. The main difference with the default Counter App is less styling and more compact code.

Note: The "flutter create" command has many more command-line arguments available.

But, what if we want to start with an even more basic, almost blank app Flutter project? When I start with the new project, I prefer to only have one Stateless widget and then build from there. Let's do that next.

A "hello world" starting project with no counter

We are going to use the same Scaffold widget sample from the previous section as a basis for an even simpler, almost empty project, which should be a nice starting point when creating projects from scratch. By stripping the counter functionality and replacing it with a single "hello world" Text widget, the end result would look like this:

Simple, almost empty 'hello world' Flutter app project

The whole code of main.dart file is as follows:

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 MaterialApp(
      title: 'Hello world project',
      home: Scaffold(
        appBar: AppBar(title: Text('Hello World App')),
        body: const Center(child: Text('Hello world.')),
        ),
    );
  }
}

Now we are down to only 20 lines of code containing one Stateless widget using the Material and Scaffold widget. It's a nice starting, generic app from which we can build-up on. Making the project even simpler would probably not make much sense.

The skeleton template

The code above is suitable when creating projects from scratch. But, what if we wanted a starting template that would also have a more advanced and complete app structure? This is where the skeleton template comes into play.

We create it by using the flutter create --template=skeleton command, but don't let the name fool you. It is more advanced than the default counter app but is not a replacement for it. Check here for a more detailed look into the skeleton template.

Conclusion

When creating a new project in Flutter with the flutter create command, it will generate a simple counter app by default. We can simplify this further by creating a project that uses --sample=material.Scaffold.1 as an argument for a version of the same counter app, but with less styling. Finally, we created a boilerplate generic, almost empty Flutter project with only one Stateful widget that could be used as a starter project.

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.