← Flutter / Dart

Flutter / Dart · 24 / 24

Measure performance and automate release checks

Compare two lookup strategies with identical results and automate verification.

  • Large
  • Advanced
  • 90 min

Prerequisites: Loops: repeat work, Riverpod: observe and change state, Unit and widget tests with meaningful expectations

In this tutorial
  1. Define the boundary
  2. Set up the learning project
  3. Trace the successful outcome
  4. Observe failures deliberately
  5. Solve the extension
  6. Compare the solution
  7. Exercise and solution
  8. Project context and sources

Download learning package

Setup and verification

Flutter 3.41.5 or newer with Dart 3.11. Python 3 is also required for the common verification entry point. Extract the learning package into a new folder and run python3 run_checks.py there. The script copies the included files into a temporary learning directory. It verifies the example and then the supplied solution there, preserving the original files. Required packages are downloaded on the first run.

Verified on 2026-10-20 on Linux x86_64. The test compares results and counted operations. Benchmark timings are observations. Flutter DevTools must additionally show whether a real interface has slow frames. The CI file describes repeatable verification and build steps.

An optimization first needs verified equivalence. We compare repeated linear lookup with an index built once. The index needs additional construction time and memory. Measurement separates construction from queries. It measures this algorithm rather than the whole Flutter interface. A workflow runs analysis, tests and a web build before any possible publication.

Package contents and individual files

01Define the boundary

Repeated list scans cost up to n comparisons per lookup. Building an index adds memory and setup cost but shortens repeated lookups. It pays off only for a suitable access pattern.

lib/model.dart · dartOpen file
List<String> linear(List<String> values, List<int> ids) => [
  for (final id in ids) values.firstWhere((value) => value == 'Vehicle $id'),
];
Map<int, String> index(List<String> values) => {
  for (var i = 0; i < values.length; i++) i: values[i],
};
List<String> indexed(Map<int, String> values, List<int> ids) => [
  for (final id in ids) values[id]!,
];

02Set up the learning project

Direct dependencies are pinned. Extract all files together and run flutter pub get. Do not use an existing app database for tests. Tests create and clean up their own data.

pubspec.yaml · yamlOpen file
name: learning_example
publish_to: none
environment:
  sdk: ">=3.11.0 <4.0.0"
dependencies:
  flutter:
    sdk: flutter
  flutter_riverpod: 3.3.1
  drift: 2.33.0
  path_provider: 2.1.5
dev_dependencies:
  flutter_test:
    sdk: flutter
flutter:
  uses-material-design: true

03Trace the successful outcome

The test compares results and counted operations. Benchmark timings are observations. Flutter DevTools must additionally show whether a real interface has slow frames. The CI file describes repeatable verification and build steps.

test/model_test.dart · dartOpen file
import 'package:flutter_test/flutter_test.dart';
import 'package:learning_example/model.dart';

void main() {
  test('index preserves order and duplicates', () {
    final values = List.generate(1000, (i) => 'Vehicle $i');
    final ids = [999, 0, 4, 4, 200];
    expect(indexed(index(values), ids), linear(values, ids));
    expect(indexed(index(values), []), isEmpty);
  });
}

Run / check

flutter pub get
flutter test
Expected result

The test compares results and counted operations. Benchmark timings are observations. Flutter DevTools must additionally show whether a real interface has slow frames. The CI file describes repeatable verification and build steps.

04Observe failures deliberately

Change an expected number or state in the test. flutter test must fail. Restore the correct expectation afterwards. Distinguish a domain failure from a missing plugin or development environment.

05Solve the extension

Add a small dataset and compare construction and repeated queries separately.

06Compare the solution

The solution makes dataset size a parameter. It claims no general speedup factor. Compare the same runtime, machine and several runs. For frame drops in an actual interface also use Flutter DevTools in profile mode.

Expected result

flutter test passes for the original example and the extended solution.

Your exercise

Add a small dataset and compare construction and repeated queries separately.

Show the solution and explanation

The solution makes dataset size a parameter. It claims no general speedup factor. Compare the same runtime, machine and several runs. For frame drops in an actual interface also use Flutter DevTools in profile mode.

solution/model_test.dart · dartOpen file
import 'package:flutter_test/flutter_test.dart';
import 'package:learning_example/model.dart';

void main() {
  test('index preserves order and duplicates', () {
    final values = List.generate(1000, (i) => 'Vehicle $i');
    final ids = [999, 0, 4, 4, 200];
    expect(indexed(index(values), ids), linear(values, ids));
    expect(indexed(index(values), []), isEmpty);
  });
}

Project context and sources

This independent learning project explores a topic in this technology area. Project references provide a pinned revision as architectural context. They do not imply that every library or feature introduced here is already used in that application.