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

Follow these steps to understand and use Dart collections:

  1. Create the playground function that will call the examples for each collection type we're going to cover:
void collectionPlayground() {
  1. First up is Lists, more commonly known as arrays in other languages. This function shows how to declare, add, and remove data from a list:
void listPlayground() {
// Creating with list literal syntax
final List<int> numbers = [1, 2, 3, 5, 7];

numbers.addAll([4, 1, 35]);

// Assigning via subscript
numbers[1] = 15;

print('The second number is ${numbers[1]}');

// enumerating a list
for (int number in numbers) {
  1. Maps store two points of data per element – a key and a value. Keys are used to write and retrieve the values stored in the list. Add this function to see Map in action:
void mapPlayground() {
// Map Literal syntax
final MapString, int ages = {
'Mike': 18,
'Peter': 35,
'Jennifer': 26,

// Subscript syntax uses the key type.
// A String in this case
ages['Tom'] = 48;

final ageOfPeter = ages['Peter'];
print('Peter is $ageOfPeter years old.');


ages.forEach((String name, int age) {
print('$name is $age years old');
  1. Sets are the least common collection type, but still very useful. They are used to store values where the order is not important, but all the values in the collection must be unique. The following function shows how to use sets:
void setPlayground() {
// Set literal, similar to Map, but no keys
final final Set<String> ministers = {'Justin', 'Stephen', 'Paul', 'Jean', 'Kim', 'Brian'};
ministers.addAll({'John', 'Pierre', 'Joe', 'Pierre'}); //Pierre is a duplicate, which is not allowed in a set.

final isJustinAMinister = ministers.contains('Justin');

// 'Pierre' will only be printed once
// Duplicates are automatically rejected
for (String primeMinister in ministers) {
print('$primeMinister is a Prime Minister.');
  1. Another Dart feature is the ability to include control flow statements directly in your collection. This feature is also one of the few examples where Flutter directly influences the direction of the language. You can include if statements, for loops, and spread operators directly inside your collection declarations. We will be using this style of syntax extensively when we get to Flutter in the next chapter. Add this function to get a feel for how control flows work on more simplistic data:
void collectionControlFlow() {
final addMore = false;
final randomNumbers = [
if (addMore) ...[

final duplicated = [
for (int number in randomNumbers) number * 2,