Book Image

Flutter Cookbook

By : Simone Alessandria, Brian Kayfitz
4 (1)
Book Image

Flutter Cookbook

4 (1)
By: Simone Alessandria, Brian Kayfitz

Overview of this book

“Anyone interested in developing Flutter applications for Android or iOS should have a copy of this book on their desk.” – Amazon 5* Review Lauded as the ‘Flutter bible’ for new and experienced mobile app developers, this recipe-based guide will teach you the best practices for robust app development, as well as how to solve cross-platform development issues. From setting up and customizing your development environment to error handling and debugging, The Flutter Cookbook covers the how-tos as well as the principles behind them. As you progress, the recipes in this book will get you up to speed with the main tasks involved in app development, such as user interface and user experience (UI/UX) design, API design, and creating animations. Later chapters will focus on routing, retrieving data from web services, and persisting data locally. A dedicated section also covers Firebase and its machine learning capabilities. The last chapter is specifically designed to help you create apps for the web and desktop (Windows, Mac, and Linux). Throughout the book, you’ll also find recipes that cover the most important features needed to build a cross-platform application, along with insights into running a single codebase on different platforms. By the end of this Flutter book, you’ll be writing and delivering fully functional apps with confidence.
Table of Contents (17 chapters)
16
About Packt

How it works...

Just like JavaScript, there are two ways of declaring string literals in Dart – using a single quote or double quotes. It doesn't matter which one you use, as long as both begin and end a string with the same character. Depending on which character you chose, you would have escaped that character if you wanted to insert it in your string.

For example, to write a string stating Dart isn't loosely typed with single quotes, you would have to write the following:

// With Single Quotes
final aBoldStatement = 'Dart isn\'t loosely typed.';

// With Double Quotes
final aMoreMildOpinion = "Dart's popularity has skyrocketed with Flutter!";

Notice how we had to write a backslash in the first example but not in the second. That backslash is called an escape character. Here, we are telling the compiler that even though it sees an apostrophe, this is not the end of the string, and the apostrophe should actually be included as part of the string.

The two ways in which you can write a string are helpful when you're writing strings that contain single quotes/apostrophes or quotation marks. If you declare your string with the symbol that is not in your string, then you will not have to add any unnecessary characters to your code, which ultimately improves legibility.

It has become a convention to prefer single quote strings over doubles in Dart, which is what we will follow in this book, except if that choice forces us to add escape characters.

One other interesting feature of strings in Dart is multi-line strings.

If you ever had a larger block of text that you didn't want to put into a single line, you would have to insert the newline character, \n, as you saw in this recipe's code:

final withEscaping = 'One Fish\nTwo Fish\nRed Fish\nBlue Fish';

The newline character has served us well for many years, but more recently, another option has emerged. If you write three quotation marks (single or double), Dart will allow you to write free-form text without having to inject any non-rendering control characters, as shown in the following code block:

final hamlet = '''
To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
And by opposing end them.
''';

In this example, every time you press Enter on the keyboard, it is the equivalent of typing the control character, \n, in your string.