Flutter Apprentice First Edition Learn To Build Crossplatform Apps 1st Edition Raywenderlich Tutorial Team Mike Katz Kevin David Moore Vincent Ngo Instant Download
Flutter Apprentice First Edition Learn To Build Crossplatform Apps 1st Edition Raywenderlich Tutorial Team Mike Katz Kevin David Moore Vincent Ngo Instant Download
https://ebookbell.com/product/flutter-apprentice-learn-to-build-
crossplatform-apps-3rd-edition-michael-katz-46274842
https://ebookbell.com/product/flutter-apprentice-fourth-edition-learn-
to-build-crossplatform-apps-4th-team-57307244
Flutter Apprentice Michael Katz Kevin David Moore Vincent Ngo Vincenzo
Guzzi
https://ebookbell.com/product/flutter-apprentice-michael-katz-kevin-
david-moore-vincent-ngo-vincenzo-guzzi-36137884
https://ebookbell.com/product/flutter-dart-cookbook-fifth-early-
release-1-20220916-fifth-early-release-richard-rose-46247992
Flutter For Dummies Barry Burd
https://ebookbell.com/product/flutter-for-dummies-barry-burd-50195624
https://ebookbell.com/product/flutter-for-beginners-an-introductory-
guide-to-building-crossplatform-mobile-applications-with-
flutter-25-and-dart-second-edition-2nd-edition-thomas-bailey-52955604
https://ebookbell.com/product/flutter-cookbook-100-stepbystep-recipes-
for-building-crossplatform-professionalgrade-apps-with-
flutter-310x-second-edition-2nd-edition-simone-alessandria-52955612
https://ebookbell.com/product/flutter-projects-a-practical-
projectbased-guide-to-building-realworld-crossplatform-mobile-
applications-and-games-1st-edition-simone-alessandria-52955630
https://ebookbell.com/product/flutter-for-beginners-crossplatform-
mobile-development-from-hello-world-to-app-release-with-
flutter-310-and-dart-3x-third-edition-3rd-edition-thomas-
bailey-53320462
Flutter Apprentice
Flutter Apprentice
By Michael Katz, Kevin David Moore & Vincent Ngo
Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.
Notice of Liability
This book and all corresponding materials (such as source code) are provided on an
“as is” basis, without warranty of any kind, express of implied, including but not
limited to the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in
the software.
Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.
raywenderlich.com 2
Flutter Apprentice
raywenderlich.com 3
Flutter Apprentice
raywenderlich.com 4
Flutter Apprentice
raywenderlich.com 5
Flutter Apprentice
raywenderlich.com 6
Flutter Apprentice
raywenderlich.com 7
Flutter Apprentice
raywenderlich.com 8
Flutter Apprentice
raywenderlich.com 9
Flutter Apprentice
raywenderlich.com 10
Flutter Apprentice
Exercise. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Switching between services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Adding streams to Bookmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Adding streams to Groceries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Chapter 15: Saving Data With SQLite . . . . . . . . . . . . . . . . . . . . . . 473
Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Adding a database to the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Adding an SQLite repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Running the app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Using Moor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
raywenderlich.com 11
Flutter Apprentice
raywenderlich.com 12
L Book License
• You are allowed to use and/or modify the source code in Flutter Apprentice in as
many apps as you want, with no attribution required.
• You are allowed to use and/or modify all art, images and designs that are included
in Flutter Apprentice in as many apps as you want, but must include this attribution
line somewhere inside your app: “Artwork/images/designs: from Flutter Apprentice,
available at www.raywenderlich.com”.
• The source code included in Flutter Apprentice is for your personal use only. You
are NOT allowed to distribute or sell the source code in Flutter Apprentice without
prior authorization.
• This book is for your personal use only. You are NOT allowed to sell this book
without prior authorization, or distribute it to friends, coworkers or students; they
would need to purchase their own copies.
All materials provided with this book are provided on an “as is” basis, without
warranty of any kind, express or implied, including but not limited to the warranties
of merchantability, fitness for a particular purpose and noninfringement. In no event
shall the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from, out of or in
connection with the software or the use or other dealings in the software.
All trademarks and registered trademarks appearing in this guide are the properties
of their respective owners.
raywenderlich.com 13
Before You Begin
This section tells you a few things you need to know before you get started, such as
what you’ll need for hardware and software, where to find the project files for this
book, and more.
raywenderlich.com 14
i What You Need
• Xcode 12.4 or later. Xcode is iOS’s main development tool, so you need it to build
your Flutter app for iOS. You can download the latest version of Xcode from
Apple’s developer site here: apple.co/2asi58y or from the Mac App Store. Xcode
12.4 requires a Mac running macOS Catalina (10.15) or later.
Note: You also have the option of using Linux or Windows, but you won’t be
able to install Xcode or build apps for iOS on those platforms.
Note: Starting in April 2021, Apple requires apps submitted to the App Store
to be developed using macOS Catalina (10.15) or later with the iOS 14
SDK.
• Flutter SDK 2.2.0 or later. You can download the Flutter SDK from the official
Flutter site at https://flutter.dev/docs/get-started/install/macos. Installing the
Flutter SDK will also install the Dart SDK, which you need to compile the Dart
code in your Flutter apps.
raywenderlich.com 15
Flutter Apprentice What You Need
• Flutter Plugin for Android Studio 56.0.2 or later, installed by going to Android
Studio Preferences on macOS (or Settings on Windows/Linux) and choosing
Plugins, then searching for “Flutter”.
You have the option of using Visual Studio Code for your Flutter development
environment instead of Android Studio. You’ll still need to install Android Studio to
have access to the Android SDK and an Android emulator. If you choose to use Visual
Studio Code, follow the instructions on the official Flutter site at https://flutter.dev/
docs/get-started/editor?tab=vscode to get set up.
Chapter 1, “Getting Started“ explains more about Flutter history and architecture.
You’ll learn how to start using the Flutter SDK, then you’ll see how to use Android
Studio and Xcode to build and run Flutter apps.
Note about null safety: At the time of publication, some of the dependencies
used throughout the book do not fully support null safety, so the code you’ll
see from Chapter 3, “Basic Widgets” on is not null safe. We’ll migrate the code
to support non-nullable types in a future update to this book.
raywenderlich.com 16
ii Book Source Code &
Forums
• https://github.com/raywenderlich/flta-materials
You can download the entire set of materials for the book from that page.
Forum
We’ve also set up an official forum for the book at https://forums.raywenderlich.com/
c/books/flutter-apprentice/. This is a great place to ask questions about the book or
to submit any errors you may find.
raywenderlich.com 17
iii About the Cover
In cultures all over the world for thousands of years, butterflies have been seen as a
symbol of mysterious transformation: The caterpillar wraps itself in silk and emerges
with wings, its original simple form now completely native to the body of a butterfly.
That’s also the magical power of Flutter — wrap your project in the framework’s
widgets and out comes something that appears endemic to iOS or Android
ecosystems from the same codebase.
raywenderlich.com 18
Flutter Apprentice About the Cover
The monarch butterfly is, of course, most famously known for its unfathomable
North American migration from Canada and the Northern United States to Mexico.
Fluttering for thousands of miles at just 5.5 miles/hour (8.85 km/hour for our global
friends), it isn’t just one butterfly making the journey at all. It takes three
generations of monarchs to make the complete migration: Those born in spring and
early summer live just 28-32 days, and those born in late summer live up to 8 months
to return home.
Luckily, your own journey will not be so long or perilous! We hope that our book
gives you all the tools you need to migrate your own projects with ease and help you
make beautiful apps that take flight.
raywenderlich.com 19
Flutter Apprentice Dedications
“To my loving parents and sister. Thank you for always being
there for me, especially during the hard times. To Ray and the
entire RW team who started me on this journey, I am beyond
grateful to be part of this family. To the Flutter community,
without whom I would have nothing to create and share here.”
— Vincent Ngo
— Michael Katz
raywenderlich.com 20
Flutter Apprentice Dedications
raywenderlich.com 21
Flutter Apprentice Dedications
Cesare Rocchi is the final pass editor for this book. Cesare runs
https://studiomagnolia.com, an interactive studio that creates
compelling web and mobile applications. He blogs at https://
www.upbeat.it. You can find him on Twitter at https://twitter.com/
_funkyboy.
raywenderlich.com 22
vi Acknowledgements
Content Development
We would like to thank Tim Sneath and Chris Sells from Google. Both provided key
insights and constant encouragement during the gestation and development of this
book.
We would also like to thank Joe Howard for his work as an FPE for the book in its
early stages. Joe’s path to software development began in the fields of computational
physics and systems engineering. He started as a web developer and also has been a
native mobile developer on iOS and Android since 2009. Joe has a passion for system
and enterprise architecture including building robust, testable, maintainable, and
scalable systems. He currently focuses on the full stack using web frameworks like
React and Angular, Node.js microservices, GraphQL, and devops tools like Docker,
Kubernetes, and Terraform. He lives in Boston and is a Senior Architect at CVS
Health.
raywenderlich.com 23
vii Introduction
Flutter is an incredible user interface (UI) toolkit that lets you build apps for iOS and
Android — and even the web and desktop platforms like macOS, Windows and Linux
— all from a single codebase.
Flutter has all the benefits of other cross-platform tools, especially because you’re
targeting multiple platforms from one codebase. Furthermore, it improves upon
most cross-platform tools thanks to a super-fast rendering engine that makes your
Flutter apps perform nearly, or even exactly, the same as native apps.
In addition, Flutter features are generally independent of native features, since you
use Flutter’s own type of UI elements, called widgets, to create your UI. And Flutter
has the ability to work with native code, so you can integrate your Flutter app with
native features when you need to.
If you’re coming from a platform like iOS or Android, you’ll find the Flutter
development experience refreshing! Thanks to a feature called “hot reload”, you
rarely need to rebuild your apps as you develop them. A running app in a simulator
or emulator will refresh with code changes automatically as you save your source
files!
In this book, you’ll see how to build full-featured Flutter apps, gain experience with a
wide range of Flutter widgets and learn how to deploy your apps to mobile app
stores.
raywenderlich.com 24
Flutter Apprentice Introduction
The next two sections focus on UI development with Flutter widgets. You’ll see just
how impressive Flutter user interfaces can be.
The fourth section switches to building a new app. You’ll use it to learn about using
networking and databases with Flutter, as well as the all-important topic of state
management.
The book’s final section shows you how to incorporate platform-specific assets into
your app, then demonstrate how to deploy your apps to the mobile app stores.
You’ll learn about where Flutter came from and why it exists, understand the
structure of Flutter projects and see how to create the UI of a Flutter app.
You’ll also get your first introduction to the key component found in Flutter user
interfaces: widgets!
Finally, you’ll dive deeper into layout widgets, scrollable widgets and interactive
widgets.
raywenderlich.com 25
Flutter Apprentice Introduction
You’ll learn about making network requests, parsing the network JSON response and
saving data in a SQLite database. You’ll also get an introduction to using Dart
streams.
Finally, this section will dive deeper into the important topic of app state, which
determines where and how to refresh data in the UI as a user interacts with your app.
Section V: Deployment
Building an app for you own devices is great; sharing your app with the world is even
better!
In this section, you’ll go over the steps and processes to release your apps to the iOS
App Store and Google Play Store. You’ll also see how to use platform-specific assets
in your apps.
raywenderlich.com 26
Section I: Build Your First
Flutter App
The chapters in this section will introduce you to Flutter, get you up and running
with a Flutter development environment and walk you through building your first
Flutter app.
You’ll learn about where Flutter came from and why it exists, understand the
structure of Flutter projects, and see how to create the user interface of a Flutter app.
You’ll also get your first introduction to the key component found in Flutter user
interfaces: Widgets!
raywenderlich.com 27
1 Chapter 1: Getting Started
By Michael Katz
Congratulations. By opening “The Flutter Apprentice”, you’ve taken your first step
toward becoming a Flutter master. This book will be your guide to learning the
Flutter UI Toolkit, Google’s platform for building apps for mobile, desktop and web
from a single codebase.
The five sections of this book will progressively teach you how to create an app using
Flutter. You’ll learn all about widgets, which are components that you compose to
build your apps. You’ll also learn about navigation and transitions, handling state
and network management. Finally, you’ll learn how to deploy the app to testers and
users.
This book assumes you’re familiar with development for a native mobile platform,
such as iOS with Swift or Android with Kotlin… but you don’t need to be an expert by
any means. These chapters will show you how to build a Flutter app from scratch, so
if you’re completely new, you’ll catch up just fine.
raywenderlich.com 28
Flutter Apprentice Chapter 1: Getting Started
What is Flutter?
In the simplest terms, Flutter is a software development toolkit from Google for
building cross-platform apps. Flutter apps consist of a series of packages, plugins and
widgets — but that’s not all. Flutter is a process, a philosophy and a community as
well.
It’s also the easiest way to get an app up and running on any one platform, let alone
multiple. You can be more productive than you thought possible thanks to Flutter’s
declarative, widget-based UI structure, first-class support for reactive programming,
cross platform abstractions and its virtual machine that allows for hot reloading of
code changes.
One thing Flutter is not is a language. Flutter uses Dart as its programming
language. If you know Kotlin, Swift, Java or Typescript, you’ll find Dart familiar, since
it’s an object-oriented C-style language.
You can compile Dart to native code, which makes it fast. It also uses a virtual
machine (VM) with a special feature: hot reload. This lets you update your code and
see the changes live without redeploying it.
For years, programmers have been promised the ability to write once and run
anywhere; Flutter may well be the best attempt yet at achieving that goal.
Seriously?
Yes, Flutter is that awesome. You can build a high-quality app that’s performant and
looks great, very quickly. This book will show you how.
In the first few chapters, you’ll get your feet under you with the basic UI. By the end
of the book, you’ll be able to build apps that look great and perform well.
And it truly does work well with both desktop and web.
raywenderlich.com 29
Flutter Apprentice Chapter 1: Getting Started
In contrast, Flutter’s widgets exist parallel to native widgets due to its custom user
interface rendering engine, Skia. That means that the toolkit controls how the UI
looks and behaves, which allows for consistent behavior between platforms. From a
performance perspective, there’s no penalty from additional layers of abstraction.
Flutter is for both the new or experienced developer who wants to start a mobile app
with minimal overhead. Flutter is for someone looking to make an app that runs on
multiple devices, either right away or in the future. It’s for someone who prefers to
build declarative UIs with the support of a large, open-source community.
Additionally, Flutter is for developers with experience on one platform who want to
develop an app that works across many. This is doubly true if you’re a web developer
with deep Javascript or Typescript knowledge, but haven’t gotten started on mobile
yet. You can learn both major mobile platforms at once!
If you don’t have an existing app, Flutter is a great way to develop something quickly
to validate an idea or to build a full, multi-platform production app.
On the other hand, if you already have a great app on one platform with the native
toolkits, then you should evaluate your ongoing maintenance costs to see if it makes
sense to build out for the other platforms by using Flutter or the native toolkits.
raywenderlich.com 30
Flutter Apprentice Chapter 1: Getting Started
• Flutter is open-source. That means you can watch its evolution and know what’s
coming — and even try out new features in development. You can also create your
own patches and packages or contribute code. And you can be involved in the
community to help others or contribute to its future direction.
• One of the best features of Flutter is hot reload. Hot reload allows you to make
updates to the code and the UI that rebuild the widget tree, then deliver them live
to emulators and devices — without having to reload state or recompile your app.
• Sometimes, you make changes that affect too much of the widget tree or app state
to hot reload easily. In those cases, you can use hot restart. Hot restart takes a
little longer than hot reload because it loads the changes, restarts the app and
resets the state, but it’s still faster than a full restart, which recompiles and
redeploys. You need to use a full restart when you make certain significant changes
to the code, including anything changing state management.
• These restart features leverage Dart’s VM to inject the updated code, so they’re
only available in debug mode and not in a production app.
• Other cross-platform toolkits produce apps with a stock look and feel — boring!
Flutter is purposely attractive, using Google’s Material Design out of the box. It’s
also easy to apply Cupertino widgets to get an iOS-like appearance. The UI is fully
customizable, allowing you make an app that looks right for your brand.
• Flutter comes with great animations and transitions, and you can build custom
widgets as well. Because widgets are composable, you can be creative and flexible
with the UI. For example, you can put videos behind a scroll view or put a toolbar
on top of a canvas.
• The sheer number of widgets and the declarative syntax for building UIs lets you
be extremely productive, building a rich app quickly with minimal overhead and
boilerplate. Stateful widgets are bound to data and automatically update as the
data model changes.
raywenderlich.com 31
Flutter Apprentice Chapter 1: Getting Started
• If you’ve used SwiftUI or Jetpack Compose recently, you’re already familiar with
many of Flutter’s concepts. But Flutter is even better — it has fewer limitations on
the tools and you can build for multiple platforms at once.
• Flutter was designed with accessibility in mind, with out-of-the-box support for
dynamic font sizes and screen readers and a ton of best practices around language,
contrast and interaction methods.
There, you see the top companies using Flutter and how diverse the apps you can
make with it are. These aren’t limited to “JSON-in-a-table” apps, but also include
media-rich dynamic and interactive apps.
These apps help you be more productive, better informed, communicate more easily
and have more fun. Flutter’s native performance and system integrations make it a
better choice than a web or hybrid app for most mobile applications.
Popular apps from some of the world’s biggest companies are built with Flutter.
These include:
• Tencent
• Realtor.com
• Google Assistant
• Policygenius
• Google Stadia
raywenderlich.com 32
Flutter Apprentice Chapter 1: Getting Started
While you can create simple 2D games using Flutter, for complex 2D and 3D games,
you’d probably prefer to base your app on a cross-platform game engine technology
like Unity or Unreal. They have more domain-specific features like physics, sprite and
asset management, game state management, multiplayer support and so on.
Flutter doesn’t have a sophisticated audio engine yet, so audio editing or mixing
apps are at a disadvantage over those that are purpose-built for a specific platform.
Flutter supports many, but not all, native features. Fortunately, you can usually
create bridges to platform-specific code. However, if the app is highly integrated with
a device’s features and platform SDKs, it might be worth writing the app using the
platform-specific tools. Flutter also produces app binaries that are bigger in size than
those built with platform frameworks.
raywenderlich.com 33
Flutter Apprentice Chapter 1: Getting Started
Flutter might not be a practical choice if you are only interested in a single platform
app and you have deep knowledge of that platform’s tools and languages. For
example, if you’re working with a highly-customized iOS app based on CloudKit that
uses all the native hardware, MLKit, StoreKit, extensions and so on, maintaining and
taking advantage of those features will be easier using SwiftUI. Of course, the same
goes for a heavily-biased Android app using Jetpack Compose.
Certain platforms
Flutter doesn’t run everywhere. It doesn’t support Apple Bitcode yet, which means
that it doesn’t support watchOS, tvOS or certain iOS app extensions. Its support for
the web is still in its early days, which means that Flutter has many features and
performance improvements ahead of it — but they’re coming.
Since Flutter doesn’t run on watches or TVs yet, you’ll have to build those
components natively and attach them to a Flutter-based mobile app. Depending on
how sophisticated those other apps are, it might not be worth the hassle to write
both native and Flutter code.
Flutter’s history
Flutter comes from a tradition of trying to improve web performance. It’s built on top
of several open-source technologies developed at Google to bring native
performance and modern programming to the web through Chrome.
The Flutter team chose the Dart language, which Google also developed, for its
productivity enhancements. Its object-oriented type system and support for reactive
and asynchronous programming gives it clear advantages over Javascript. Most
importantly, Google built the Dart VM into the Chrome browser, allowing web apps
written in Dart to run at native speeds.
Another piece of the puzzle is the inclusion of Skia as the graphics rendering layer.
Skia is a another Google-based open source project that powers the graphics on
Android, Chrome browsers, Chrome OS and Firefox. It runs directly on the GPU using
Vulcan on Android and Metal on iOS, making the graphics layer fast on mobile
devices. Its API allows Flutter widgets to render quickly and consistently, regardless
of its host platform.
raywenderlich.com 34
Flutter Apprentice Chapter 1: Getting Started
1. The Framework layer is written in Dart and contains the high-level libraries that
you’ll use directly to build apps. This includes the UI theme, widgets, layout and
animations, gestures and foundational building blocks. Alongside the main
Flutter framework are plugins: high-level features like JSON serialization,
geolocation, camera access, in-app payments and so on. This plugin-based
architecture lets you include only the features your app needs.
2. The Engine layer contains the core C++ libraries that make up the primitives that
support Flutter apps. The engine implements the low-level primitives of the
Flutter API, such as I/O, graphics, text layout, accessibility, the plugin
architecture and the Dart runtime. The engine is also responsible for rasterizing
Flutter scenes for fast rendering onscreen.
3. The Embedder is different for each target platform and handles packaging the
code as a stand-alone app or embedded module.
raywenderlich.com 35
Flutter Apprentice Chapter 1: Getting Started
Each of the architecture layers is made up of other sublayers and modules, making
them almost fractal. Of particular import to general app development is the makeup
of the framework layer:
• At the top is the UI theme, which uses either the Material (Android) or Cupertino
(iOS) design language. This affects how the controls appear, allowing you to make
your app look just like a native one.
• The widget layer is where you’ll spend the bulk of your UI programming time.
This is where you compose design and interactive elements to make up the app.
• Beneath the widgets layer is the rendering layer, which is the abstraction for
building a layout.
• The foundation layer provides basic building blocks, like animations and
gestures, that build up the higher layers.
raywenderlich.com 36
Flutter Apprentice Chapter 1: Getting Started
What’s ahead
This book is divided into five sections:
• Section 1 is the introduction. You’re here! In this section, you’ll get an overview
of Flutter, learn how to get started and make sure you have everything set up to
develop great apps. You’ll build a simple app to get a taste of the Dart language
and Flutter SDKs.
• Section 2 is all about widgets, the building blocks you use to make your app.
• Section 3 covers navigation and deep links. If you think about widgets as making
up screens, navigation ties them together to let the user accomplish various tasks
within the app.
• Section 4 goes over state and data. You’ll learn how to save data and work with
local persistence and networking.
• Section 5 shows you how to make the app work with native platforms as well as
how to deploy your app.
By the end of the book, you’ll be able to take an idea, turn it into a great-looking
multi-platform app and submit it for publication.
Getting started
Now that you’ve decided Flutter is right for you, your next step is to get the tools
necessary to build Flutter apps: the Flutter SDK and Dart compiler. You’ll also need
an IDE with a Flutter plugin along with the tools to build and deploy for the various
platforms. The latter means Xcode for iOS and Android Studio for Android.
To start, visit https://flutter.dev/. This portal is the source of truth for any
installation instructions or API changes that occur between this book’s publication
and the time you read it. If there are any contradictions, the information at
flutter.dev supersedes.
raywenderlich.com 37
Flutter Apprentice Chapter 1: Getting Started
Note: Because of the Xcode limitation for macOS, this book uses the Flutter
toolchain on Mac. You can follow along on any platform of your choice — just
skip any iOS- or Mac-specific steps.
• At least one device. You can run in an iOS Simulator or Android emulator, but
running Flutter apps on a physical device will give you the true user experience.
• Developer accounts (optional). To deploy to the Apple App Store or Google Play
Store, you’ll need a valid account on each.
One thing to note is that Flutter organizes its SDK around channels, which are
different development branches. New features or platform support will be available
first on a beta channel for developers to try out. This is a great way to get early
access to certain features like new platforms or native SDK support.
For this book and development in general, use the stable channel. That branch has
been vetted and tested and has little chance of breaking.
raywenderlich.com 38
Flutter Apprentice Chapter 1: Getting Started
Once you do that, you’ll have access to the Flutter command-line app, which is your
starting point. To check you’ve set it up correctly, run the following command in a
terminal:
flutter help
Common commands:
These flutter subcommands are a gateway to all the tools that come with Flutter.
You’ll see project management tools, package management tools and tools to run
and test your apps. You’ll dive into many of these in this and future chapters.
Just run:
flutter doctor
That checks for all the necessary components and provides the links or instructions
to download ones you’re missing.
Here’s an example:
Doctor summary (to see all details, run flutter doctor -v):
Unable to find any JVMs matching version "1.8".
No Java runtime present, try --request to install.
[✓] Flutter (Channel stable, 2.0.6, on Mac OS X 10.15.7, locale
en-US)
[✗] Android toolchain - develop for Android devices
raywenderlich.com 39
Flutter Apprentice Chapter 1: Getting Started
In this example output, Flutter Doctor has identified a series of issues: mainly, no
Java, an outdated Android toolchain and that CocoaPods, Android Studio and Google
Chrome are missing.
The tool has helpfully suggested commands and links to get the missing
dependencies. The tool also terminated before completing, which is common if it
doesn’t find major dependencies.
For your specific setup, follow the suggestions to install whatever you’re missing.
Then keep running flutter doctor until you get all green checkmarks. You’ll likely
have to run it more than a couple of times to clear all the issues.
raywenderlich.com 40
Flutter Apprentice Chapter 1: Getting Started
Note: If Flutter Doctor’s suggestions don’t work, you may have to manually
install missing tools, like Java or Android Studio, by following the instructions
on their respective websites. Just take it one step at a time. Setting up the
development environment is the hardest part of working with Flutter.
Setting up an IDE
The Flutter team officially supports three editors: Android Studio, Visual Studio
Code and Emacs. However, there are many other editors that support the Dart
language, work with the Flutter command line or have third-party Flutter plugins.
This book’s examples use Android Studio, but the code and examples will all work in
your editor of choice. Flutter Doctor will have you install this IDE anyway, to get all
the Android tools, so using Android Studio keeps you from having to install
additional editors. Additionally, Flutter Doctor will tell you to install the Android
Studio Flutter plugin, which also triggers an install of the Dart plugin for Android
Studio.
Once you go through all of the flutter doctor steps, you’ll have everything you
need to create Flutter apps in Android studio. If you see Create New Flutter project
in the Android Studio welcome window, you’re good to go.
raywenderlich.com 41
Flutter Apprentice Chapter 1: Getting Started
Trying it out
Downloading all the components is the hardest part of getting a Flutter app up and
running. Next, you’ll try actually building an app.
There are two recommended ways to create a new project: with the IDE or through
the flutter command-line tool in a terminal. In this chapter, you’ll use the IDE
shortcut and in the next chapter, you’ll use the command line.
In Android Studio, click the Create New Flutter Project option. Leave the default
app selected and click the Next button to continue to the next screen.
For this example, you can keep the default values or change to something more
convenient. Click the Next button to continue.
The options here let you include platform support or change the package name.
You’ll learn more about these options later. For now, click the Finish button.
raywenderlich.com 42
Flutter Apprentice Chapter 1: Getting Started
If you use Visual Studio Code, the process is similar. To create a new project, use
View ▸ Command Palette… ▸ Flutter: New Project. After that, click through the
project form that comes up.
With either editor, you might see pop-ups or messages to download or update
various tools and components. Follow the directions until you resolve the messages.
For example, this Android Studio banner shows: ‘Pub get’ has not been run.
Clicking Get dependencies resolves this.
raywenderlich.com 43
Flutter Apprentice Chapter 1: Getting Started
It might take a while to compile and launch the first time. When you’re done, you’ll
see the following:
Congratulations, you’ve made your first Flutter app! Click the button and see the
increment response update the label.
raywenderlich.com 44
Flutter Apprentice Chapter 1: Getting Started
All the code for this app is in lib\main.dart in the default project. Feel free to take a
look at it.
Throughout the rest of this book, you’ll dive into Flutter apps, widgets, state, themes
and many other concepts that will help you build beautiful apps.
Text(
'You have pushed the button this many times:',
),
Next, change the string to: ‘Thou hast pushed the button this many times:’ to
give it a faux-medieval flair.
Here’s the not-so-tricky part: Just save the file. Now, look at the running app and
observe the change.
Et voila! Your changes reload without stopping the app and redeploying.
raywenderlich.com 45
Flutter Apprentice Chapter 1: Getting Started
Sometimes, saving the file does not automatically trigger the hot reload. In that case,
just press the Hot Reload icon, which looks like a lightning bolt, in the toolbar.
Key points
• Flutter is a software development toolkit from Google for building cross-
platform apps using the Dart programming language.
• With Flutter, you can build a high-quality app that’s performant and looks great,
very quickly.
• Flutter is for both new and experienced developers who wants to start a mobile
app with minimal overhead.
• Install the Flutter SDK and associated tools using instructions found at https://
flutter.dev.
• The flutter doctor command helps you install and update your Flutter tools.
• This book will mostly use Android Studio as the IDE for Flutter development.
raywenderlich.com 46
Flutter Apprentice Chapter 1: Getting Started
flutter.dev contains the official documentation and reference pages, which you can
find at https://flutter.dev/docs. These will be your source for complete and up-to-
date information about the SDKs.
Also, there’s https://flutter.dev/community, which has links to all the official Flutter
communities on multiple social media platforms. In particular, check out the Google
Developers’ Flutter YouTube channel: https://www.youtube.com/c/flutterdev/.
raywenderlich.com 47
2 Chapter 2: Hello, Flutter
By Michael Katz
Now that you’ve had a short introduction, you’re ready to start your Flutter
apprenticeship. Your first task is to build a basic app from scratch, giving you the
chance to get the hang of the tools and the basic Flutter app structure. You’ll
customize the app and find out how to use a few popular widgets like ListView and
Slider to update its UI in response to changes.
Creating a simple app will let you see just how quick and easy it is to build cross-
platform apps with Flutter — and it will give you a quick win.
By the end of the chapter, you’ll have built a lightweight recipe app. Since you’re just
starting to learn Flutter, your app will offer a hard-coded list of recipes and let you
use a Slider to recalculate quantities based on the number of servings.
raywenderlich.com 48
Flutter Apprentice Chapter 2: Hello, Flutter
All you need to start this chapter is to have Flutter set up. If the flutter doctor
results show no errors, you’re ready to get started. Otherwise, go back to Chapter 1,
“Getting Started”, to set up your environment.
Open a terminal window, then navigate to the location where you want to create a
new folder for the project. For example, you can use this book’s materials and go to
flta-materials/02-hello-flutter/projects/starter/.
This command creates a new app in a new folder, both named recipes. It has the
demo app code, as you saw in the previous chapter, with support for running on iOS
and Android.
raywenderlich.com 49
Flutter Apprentice Chapter 2: Hello, Flutter
Build and run and you’ll see the same demo app as in Chapter 1, “Getting Started”.
raywenderlich.com 50
Discovering Diverse Content Through
Random Scribd Documents
"You have suffered from my frantic accusations on that dreadful
July day, but you will never understand the intolerable bitterness of
my punishment, scourged all these dreary, mournful years by keen,
torturing self-reproach. Roy—my own Roy—I am not worthy, but the
world is empty and desolate for me without the one love of my life."
Transcriber Notes:
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
ebookbell.com