Flutter Tutorial: Step-By-Step Guide To Kick Off Your First Flutter Project
Flutter Tutorial: Step-By-Step Guide To Kick Off Your First Flutter Project
What is Flutter
Flutter Live on 4 December 2018, with loads of new features. After this, the
popularity of Flutter apps grew drastically.
Installing Software
In order to get started with Flutter, we have to install some software on your
local machine. You can either use macOS, Windows or Linux to get started
with Flutter, however, macOS has the bene t of getting both the iOS and
Android environment on the same machine. We will assume that you are
using macOS for Flutter development, but you can always follow the similar
instructions for Windows and Linux as well.
Install Flutter
$ cd ~/development
Select category
$ unzip ~/Downloads/flutter_macos_v1.5.4-hotfix.2-stable.zipSearch articles
$ unzip flutter_macos_v0.7.3-beta.zip
Now that we have downloaded the Flutter SDK, we need to add Flutter to our
$PATH so that we can access Flutter globally. This varies from shell to shell,
bash, we can simply run this command:
$ export PATH=[PATH_TO_FLUTTER__DIRECTORY]/flutter/bin:$PATH
You can nd the instructions to update the Flutter PATH here. Once you set
up the PATH, you should then be able to con rm installation by running:
$ flutter --help
Once Flutter is installed, you may also want to install Dart. Usually, some
Dart libraries come with Flutter itself, but if you need more Dart developer
tools then you probably need to install Dart as well. This is optional but good
to have. You need to install Homebrew in order to get Dart on your machine.
You can install Homebrew by using the following command:
This will take some time, but once the installation is complete, you should
be able to install any package using Homebrew. Let's install Dart by using
following commands:
Install Xcode
You can develop both iOS and Android apps with Flutter. If you are
developing an iOS app with Flutter, you need to install Xcode on your macOS.
You can not install on other platforms like Windows or Linux. On macOS, it's
very easy to install Xcode, you simply need to use your Apple developer
account and download it from the App Store. Xcode is a large piece of
software so it will take some time to download. Once you have Xcode
installed, you will get most of the command line tools along with Xcode. We
might need to con gure the Xcode command line tools to use the newly-
installed version of Xcode by using the command:
Once the command line tools install, you need to agree on the Xcode terms
and conditions. You can do so by opening Xcode and clicking on the Agree
button or by con rming or running sudo xcodebuild - license from the
command line.
With Xcode, you'll be able to run Flutter apps on an iOS device or on the
simulator. The complete guide for the iOS setup is available in the Flutter
documentation, here.
If you are developing apps for Android, you also need to complete Android
setup. If you download and install Android Studio then most of the basic
setup is covered. Once you start Android Studio, go through the ‘Android
Studio Setup Wizard’. This installs the latest Android SDK, Android SDK
Platform-Tools and Android SDK Build-Tools, which are required by Flutter
when developing for Android. You can also set up a device or emulator for
the Android development as mentioned in the Flutter documentation, here.
Flutter uses the Pub Package Manager to manage the Dart packages inside
the Flutter project. You will nd the pubspec.yaml le in the Flutter project
which imports Dart dependencies inside the Flutter project. You can
mention all the dependencies in the le and install using the command:
This will install all the dependencies and create a locked version of the le,
which is pubspec.lock , so that we will always lock dependencies. We can
upgrade the dependencies using the command:
This will upgrade all packages to the latest version, or you can update
individual packages.
It's good to know the concept of Pub Package Manager well in advance in
It s good to know the concept of Pub Package Manager well in advance in
order to understand the Flutter project better.
At this point, you will be able to start developing your rst Flutter app. There
are multiple ways to create a template app, such as using the command line,
using Android Studio or using IntelliJ. However, we will use the command
line way in order to understand the entire Flutter project. Flutter can
generate a boilerplate for the sample app by running the following
commands:
You can give any name to your app e.g utter_testing or hello_world. Let's
say “ utter_test” for now.
This command will create the boilerplate of the Flutter app inside the
utter_test directory. You can run your rst ever Flutter app using the
commands. However, you need to have a device/simulator running to see the
app.
$ cd flutter_test
$ flutter run
You can see the app running in the emulator/simulator or devices. This
would be the default Flutter app which counts how many times you pressed
the + button. The app looks like this in the simulator:
We have a dedicated directory for the iOS and Android app. Some
con guration les like pubspec.yaml, .gitignore, etc are there at the root of
the project. We have two main directories where most of our Flutter code
will live, i.e. lib and test . In the “lib” directory we have main.dart where
our app code lives, and we can write the tests in the “test” directory. In the
main dart le you can create widgets and develop Flutter apps as per your
main.dart le, you can create widgets and develop Flutter apps as per your
needs. You can refer to the great documentation of Flutter.
As you develop the Flutter app, it's great practice to test the app with a solid
set of automated tests. Flutter has a richer set of testing features than any
other cross-platform mobile app development framework. Flutter provides
a solid testing framework which allows developers to write tests at the unit,
functional and UI level. Widget testing is one cool feature that Flutter
provides to run UI tests as fast as unit tests. Flutter also has UI tests, known
as integration tests, that run on the simulator or on real devices. Flutter has
great documentation on how to test Flutter apps at di erent levels. You can
read our post on Full Stack testing of the Flutter apps to gain a practical
insight on Flutter testing.
Now let's add Widget tests for the demo app, which check the initial state of
the app, tap the + button and assert that the state has been and counter
incremented. The test will look something like this.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_testing/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
Select category Search articles
});
}
Let's name this test widget_test.dart and keep it inside the test directory.
We can run the test from the command line using the flutter test
command which will execute all tests from the test directory. However, we
can execute a single test using the command
Now that we have added a test to our Flutter app, the next step is to run it
continuously on the CI server so that we will receive feedback if our
functionality continues to work with the latest code changes.
Subscribe to our newsletter
Select category Search articles
Setting up Continuous Integration for Flutter apps isn't easy if you want to
do it yourself on the blank server. The Flutter ecosystem has a lot of moving
parts and tools that you need to install before you can actually start your
Flutter build process. The steps include provisioning Flutter installation,
Dart VM, installing iOS and Android dependencies, installing Dart
dependencies, etc. Luckily, Codemagic has solved all our problems for us.
Codemagic does all this work for us; simply visit the codemagic.io website
and register yourself with your GitHub, GitLab or Bitbucket account and
point your app to Codemagic. You can nd the getting started guide to set up
your app using Codemagic. The default work ow of Codemagic is to build,
test and publish the artefacts of your iOS and Android builds. We have our
source code of the demo test app utter testing on Github, we have built the
“CI” branch and we are following as a build output without any
con guration.
Select category Search articles
We have now successfully added our app to the Codemagic CI/CD server. In
order to know more, refer to the Codemagic documentation.
Conclusion
Flutter From Scratch, Episode 1: Installing Flutter and Con guring
Android Studio
Testing Publishing Building CI/CD Native IOS Native Android React Native
Getting Started with Codemagic CI/CD for mobile
Step by step guide how to set up Codemagic CI/CD for Flutter,
native iOS, native Android and React Native apps.
Select category Search articles
Latest articles
Product Company
Pricing Careers
Status
© Nevercode Ltd. | All Rights Reserved | Codemagic is registered trademark of Nevercode Ltd.