Guide
Enabling Flutter Null Safety in an Existing Project
Step‑by‑step guide to enable Flutter null safety, including migration, configuration, testing, and rollback procedures.
Published by Tasadduq Burney
05 Aug 2025, 10:11 UTC
3 min124K views0

Desired outcome
Enable sound null safety for a Flutter project so that the Dart analyzer treats all types as non‑nullable by default, reducing runtime null‑reference errors and unlocking IDE assistance.
Prerequisites
- Flutter SDK version >=2.0.0 (null safety support landed in Flutter 2).
- Dart SDK version >=2.12.0 (included with Flutter 2).
- A version‑controlled repository (e.g., Git) with a clean working directory or a recent commit you can return to.
- Access to run
flutteranddartcommands from the project root.
Procedure
- Create a backup branch or tag. From the project root run:
This isolates the migration work.git checkout -b null-safety-migration - Run the automated migration tool. Execute:
The tool updates your source files and generates a diff. Review the changes carefully; it will also attempt to update dependencies to null‑safe versions where possible.flutter pub migrate - Resolve remaining opt‑outs. After the migration finishes, you may see files with
// ignore: avoid_asor similar comments, or// @dart=2.9opt‑out markers. Remove these markers only after you have verified that the code no longer needs them, or keep them temporarily if a dependency still requires the old syntax. - Update SDK constraints. In
pubspec.yaml set the environment SDK constraint to a null‑safe range, for example:
If you are using a newer Flutter stable channel, you can extend the upper bound accordingly (e.g.,environment: sdk: ">=2.12.0 <3.0.0"<=3.0.0or omit it). - Configure the analyzer. Ensure
analysis_options.yaml contains:
If you rely on the default SDK constraint, theanalyzer: enable-experiment: - non-nullable language: strict-cases: true linter: rules: - avoid_null_checks_in_equality - unnecessary_null_comparisonenable-experimentline can be omitted, but keeping it makes the intent explicit. - Run a full analysis. From the project root:
orflutter analyzedart analyze. The output should show zero null‑safety warnings or errors. - Execute the test suite. Run unit, widget, and integration tests:
If any test fails because of a null‑safety issue, fix the offending code before proceeding.flutter test - Verify dependency compatibility. Check that all direct and transitive dependencies are null‑safe:
Any package reported as “pre‑null‑safety” must be upgraded, forked, or patched.flutter pub outdated --mode=null-safety
Expected checks
flutter analyzereturns an empty warning list (or only non‑null‑safety related hints).- The
flutter pub outdated --mode=null-safetycommand shows no packages with a red “pre‑null‑safety” flag. - All tests pass (
flutter testexit code 0). - The application builds successfully for at least one target (e.g.,
flutter build apk --debugorflutter run).
Recovery options
If a critical issue appears after migration:
- Revert to the backup branch:
or the branch you created before migration.git checkout main - Alternatively, reset the migration commit:
(adjust the number of commits as needed).git reset --hard HEAD~1 - Re‑apply any local changes that were not committed, then re‑evaluate the migration steps or seek updated versions of problematic dependencies.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.