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 to do it...

Just like in the previous project, you are going to create a playground function where every sub-function will demonstrate a different aspect of the strings:

  1. Type in the following code and use it as the hub for all the other string examples:
void stringPlayground() {
basicStringDeclaration();
multiLineStrings();
combiningStrings();
}
  1. The first section demonstrates the ways in which you can declare string literals. Write the following function into your code, just under the stringPlayground function:
void basicStringDeclaration() {
// With Single Quotes
print('Single quotes');
final aBoldStatement = 'Dart isn\'t loosely typed.';
print(aBoldStatement);

// With Double Quotes
print("Hello, World");
final aMoreMildOpinion = "Dart's popularity has skyrocketed with
Flutter!";
print(aMoreMildOpinion);
// Combining single and double quotes
final mixAndMatch =
'Every programmer should write "Hello, World" when learning
a new language.';
print(mixAndMatch);
}

  1. Dart also supports multi-line strings for cases where you have a text block that you want to print to the screen. The following example gets a little Shakespearean:
void multiLineStrings() {
final withEscaping = 'One Fish\nTwo Fish\nRed Fish\nBlue Fish';
print(withEscaping);

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.
''';

print(hamlet);
}
  1. Finally, one of the most common tasks programmers perform with strings is composing them to make more complex strings. Dart supports both the traditional method of concatenation, as well as a more modern method called string interpolation. Type in the following blocks of code to get a feel for both techniques:
void combiningStrings() {
traditionalConcatenation();
modernInterpolation();
}

void traditionalConcatenation() {
final hello = 'Hello';
final world = "world";

final combined = hello + ' ' + world;
print(combined);
}

void modernInterpolation() {
final year = 2011;
final interpolated = 'Dart was announced in $year.';
print(interpolated);

final age = 35;
final howOld = 'I am $age ${age == 1 ? 'year' : 'years'} old.';
print(howOld);
}

  1. Now, all we have to do to run this code is update main.dart so that it points this file to a new file. Replace the top of main.dart with the following code:
main() {
variablePlayground();
stringPlayground();
}