How to create a new Flutter project

Creating a new Flutter project is fundamental to building our first Flutter app. Flutter is a powerful open-source UI framework developed by Google, allowing developers to make beautiful, fast, cross-platform applications.

Prerequisites

Before diving into the process, there are a few prerequisites that we'll need to set up:

  • Install Flutter SDK

  • Install Android Studio

  • Download and Install JDK(Java SE Development Kit)

  • Install a suitable IDE like Android Studio, Visual Studio Code, or any other editor of your preference. 

  • A device or emulator to run your app on.

For detailed installation guide of Flutter, click here.

Using Command Line

Once we have mentioned prerequisites in place, we can proceed with creating a new Flutter project using the following steps:

  1. Navigate to the directory where we want to create our Flutter project using the cd command:

cd /path/to/desired/location
  1. Create a new Flutter project using the following command:

flutter create my_project_name

When creating a Flutter project, use all lowercase letters, separate words with underscores, and only use letters (a-z, A-Z), digits (0-9), and underscores. Using uppercase letters, dashes, or other special symbols in the project name will result in an error stating ... is not a valid Dart package name.

  1. We can now navigate to the project folder and run the project using the following command and make sure we have a device or emulator set up:

cd my_project_name
flutter run

Optional arguments

We can pass some optional arguments to the command to customize our project according to the specific requirements.

Setting language for Android

We can use the --android-language argument (short: -a) to choose between Java and Kotlin for Android-specific applications. The default is kotlin.

flutter create --android-language=java my_project_name

Setting language for iOS

We can use the --ios-language argument (short: -i) to choose between Swift and Objective-C for iOS-specific applications. The default is swift.

flutter create --ios-language=objc my_project_name

Setting target platforms

By default, Flutter sets up the project for all platforms, such as androidwebioswindowslinux, and macos. However, we can specify the platforms using the --platforms argument. Separate multiple values with commas.

flutter create --platforms=ios,web my_project_name

Setting project type

We can specify the project type using the --template argument (short: -t), as it would be better if the project is created according to the project type we will work on. Options include app, module, package, and plugin.

flutter create --template=plugin my_project_name

--platforms argument will only work if –template argument will have value app(default) or plugin

Setting code sample

We can provide a code sample for the project using the --sample argument (short: -s). Pass the sample ID as the argument value.

flutter create --sample=material.TweenAnimationBuilder.1 my_project_name

We can also view all the available samples using the following command:

flutter create --list-samples=list_samples.json

Conclusion

So that's how we can create a new Flutter project using the command line. Flutter allows us to specify the used language for Android and iOS, specify the target platforms, set the project type, and select the code sample.

Additional resources

After successfully creating the project, we can learn about different Flutter packages from the following resources:

Copyright ©2024 Educative, Inc. All rights reserved