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)
About Packt

How to do it...

Let's get started with our first Dart project. We will start from a blank canvas:

  1. Open main.dart and delete everything. At this point, the file should be completely empty. Now, let's add the main function, which is the entry point for every Dart program:
main() {
  1. This code won't compile yet because we haven't defined that variablePlayground function. This function will be a hub for all the different examples in this recipe:
void variablePlayground() {

We added the void keyword in front of this function, which is the same as saying that this function returns nothing.

  1. Now, let's implement the first example. In this method, all these variables are mutable; they can change once they've been defined:
void basicTypes() {
int four = 4;
double pi = 3.14;
num someNumber = 24601;
bool yes = true;
bool no = false;
int nothing;

print(nothing == null);

The syntax for declaring a mutable variable should look very similar to other programming languages. First, you declare the type and then the name of the variable. You can optionally supply a value for the variable after the assignment operator. If you don't supply a value, that variable will be set to null.

  1. Dart has a special type called dynamic, which is a sort of "get out of jail free" card from the type system. You can annotate your variables with this keyword to imply that the variable can be anything. It is useful in some cases, but for the most part, it should be avoided:
void untypedVariables() {
dynamic something = 14.2;
print(something.runtimeType); //outputs 'double'
  1. Dart can also infer types with the var keyword. var is not the same as dynamic. Once a value has been assigned to the variable, Dart will remember the type and it cannot be changed later. The values, however, are still mutable:
void typeInterpolation() {
var anInteger = 15;
var aDouble = 27.6;
var aBoolean = false;



  1. Finally, we have our immutable variables. Dart has two keywords that can be used to indicate immutability  final and const

The main difference between final and const is that const must be determined at compile time; for example, you cannot have const containing since the current date and time can only be determined at runtime, not at compile time. See the How it works... section of this recipe for more details.
  1. Add the following function to the main.dart file:
void immutableVariables() {
final int immutableInteger = 5;
final double immutableDouble = 0.015;

// Type annotation is optional
final interpolatedInteger = 10;
final interpolatedDouble = 72.8;


const aFullySealedVariable = true;