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

Let's see an example of null unsafe code, and then fix it. To do that, follow these steps: 

  1. In DartPad, make sure Null Safety is disabled. You can toggle Null Safety with the control at the bottom of the screen:

  1. Remove the default code in the main method, and add the following instructions:
void main() {
int someNumber;
increaseValue(someNumber);
}
  1. Create a new method under main that takes an integer and prints the value that was passed, incremented by 1:
void increaseValue(int value) {
value++;
print (value);
}
  1. Run your code. You should see a null error in the console, as shown in the following screenshot:

  1. Enable Null Safety with the switch at the bottom of the screen, and note that someNumber at line 3 raises a compile error before execution on someNumber: "The non-nullable local variable 'someNumber' must be assigned before it can be used.
  2. Add a question mark after the two int delcarations:
void main() {
int? someNumber;
increaseValue(someNumber);
}

void increaseValue(int? value) {
value++;
print (value);
}
  1. Note that the error has changed to: "The method '+' can't be unconditionally invoked because the receiver can be 'null'."
  2. Edit the increaseValue method, so that you check whether the value is null before incrementing it, otherwise you just return 1:
void increaseValue(int? value) {
if (value != null) {
value++;
} else {
value = 1;
}
print (value);
}
  1. Run the app and note that you find the value 1 in the console. 
  2. Edit the increaseValue method again. This time, use the null-check operator:
void increaseValue(int? value) {
value = value ?? 0;
value++;
print (value);
}
  1. Run the app, and note that in the console you still find the value 1.
  2. Remove the question mark from the value parameter, and force the call to increaseValue with an exclamation mark: 
void main() {
int? someNumber;
increaseValue(someNumber!);
}

void increaseValue(int value) {
value++;
print (value);
}
  1. Run the app, and note that you get an execution null exception.
  2. Finally, fix the code by initializing someNumber with an integer value:
void main() {
int someNumber = 0;
increaseValue(someNumber);
}

void increaseValue(int value) {
value++;
print (value);
}
  1. Now you should see the value 1 in the console again.