Sub topic 3
Measuring startup with --trace-startup and DevTools
This chapter assumes you can run a Flutter app, edit pubspec.yaml, use async and await in Dart, and that you know Flutter has debug, profile, and release build modes.
Measuring startup with --trace-startup and DevTools
Measure startup only in profile or release mode. Debug mode adds checks that slow the app and distort the numbers.
flutter run --profile --trace-startupThis command writes timing data to build/start_up_info.json. Open Flutter DevTools and use the Performance view to inspect the timeline. Record the time to first frame before you change any code. That number is your baseline.
Keeping main() and the first frame lean
Initialize only what the first screen needs before runApp. Start everything else after the first frame is scheduled.
import 'dart:async';
import 'package:flutter/widgets.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
WidgetsBinding.instance.addPostFrameCallback((_) {
unawaited(initAnalytics());
});
}
Future<void> initAnalytics() async {
// Placeholder for non-critical setup.
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const Directionality(
textDirection: TextDirection.ltr,
child: Center(child: Text('Hello')),
);
}
}Moving heavy work to isolates
Dart runs your UI on one isolate. A long computation on that isolate blocks frame drawing. Isolate.run executes a function on a separate isolate and returns the result.
import 'dart:isolate';
List<int> parseNumbers(String raw) {
return raw.split(',').map(int.parse).toList();
}
Future<List<int>> loadNumbers(String raw) {
return Isolate.run(() => parseNumbers(raw));
}Perceived startup: native splash and first screen design
The native splash screen appears before Flutter draws its first frame. Keep the first screen simple, and show its layout as soon as possible, even if some data is still loading.
Keeping the gains: budgets and CI checks
Save your baseline numbers in the repository. Then add a CI step that fails when a measurement exceeds its budget.
| Metric | Baseline (example) | Budget (example) |
|---|---|---|
| Time to first frame | 900 ms | 1000 ms |
| Release APK size | 18 MB | 19 MB |
Exercise
You measured a time to first frame of 1.2 s in profile mode. A teammate measured 2.5 s in debug mode. Which number should you use as the baseline, and why?
Show answer
Use the 1.2 s profile measurement. Debug mode adds checks and runs slower, so its timings do not reflect what users experience.