Flutter Debugging and Performance Profiling
This purpose of this chapter is to help you debug, diagnose issues with and profile your Flutter app. Flutter gives us amazing tools for this purpose, which can provide you with any information you should require. In fact, almost too much information! This is a very deep subject and the most this chapter can do is ‘dip your toe in the water’.
Flutter Debugging & Profiling is quite a rabbit-hole!
Obviously you should be running Flutter in checked mode.
When you are profiling, you should ensure the following:
You are connected to a real device.
An emulator can ‘emulate’ the real thing but under the covers it’s not the same thing.
You are running Flutter in profile mode.
This mode was written especially for this task, with enough performance to simulate release mode but enough information to help you profile the app.
Programmatical OptionsWhen you write code, it has a purpose – to perform a certain task.
However, you can augment that code with additional code that helps you diagnose issues and profile your Flutter app:
Debugger Statements. When you are debugging and attempting to reproduce a condition, you can add temporary code to detect that condition and launch the debugger.
Print to the Console.
You can output to the console to provide runtime information about what is happening in the program, what are variable values set to.
Assertions.You can add assertions to enable programs to detect their own defects.
statement. This statement has an optional ‘when’ argument which you can specify to only break when a certain condition is true.
Remember to import ‘dart:developer’ at the top!
Exercise – ‘debugging’This exercise involves the default Flutter app modified to do into debug mode when the counter reaches 5.
Step 1 – Create Default Flutter AppFollow the instructions in Generate Your First App Leave project open.
Step 2 – Replace Application CodeReplace contents of file ‘main.dart’ in folder ‘lib’ with the following:
Step 3 – Open Emulator & Run
Follow the instructions in Open Android Emulator & Run Your First App
Run the app in debug mode and hit the ‘+’ floating button until your IDE goes into debug mode and highlights the line containing ‘debugger’.