Null Safety Dependencies Error in Flutter
When migrating your Flutter application to null safety, you might encounter the error: "Cannot run with sound null safety, because the following dependencies don't support null safety." This occurs when your app uses packages that haven't yet been migrated to support Dart's null safety feature.
Problem: Mixed Null Safety Environments
Flutter's sound null safety prevents apps from running when dependencies aren't fully migrated. Common packages that previously caused this issue included:
cloud_firestore_web
firebase_core_web
shared_preferences
url_launcher_web
firebase_auth
http
provider
Note
Most of these packages now support null safety, but you may still encounter similar issues with other dependencies.
Solutions
1. Update Dependencies (Recommended)
Before using workarounds, check if updated null-safe versions of your dependencies exist:
# pubspec.yaml
dependencies:
# Check for null-safe versions:
shared_preferences: ^2.0.0
http: ^0.13.0
provider: ^6.0.0
Check for null-safe packages using:
dart pub outdated --mode=null-safety
2. Run with Unsound Null Safety (Temporary Fix)
To run your app with mixed null safety:
flutter run --no-sound-null-safety
For specific build targets:
# For APK
flutter build apk --no-sound-null-safety
# For App Bundle
flutter build appbundle --no-sound-null-safety
# For iOS
flutter build ipa --no-sound-null-safety
# For specific devices
flutter run -d chrome --no-sound-null-safety
3. IDE Configuration
{
"dart.flutterRunAdditionalArgs": ["--no-sound-null-safety"],
"dart.flutterTestAdditionalArgs": ["--no-sound-null-safety"]
}
{
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
"args": ["--no-sound-null-safety"]
}
]
}
Android Studio/IntelliJ:
- Open Run → Edit Configurations
- Add
--no-sound-null-safety
to "Additional run args"
4. File-level Opt-Out (Legacy Approach)
Add this directive at the top of your main.dart
file:
// @dart=2.9
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
Warning
This approach uses legacy Dart 2.9 behavior and is not recommended for new projects. Use temporary runtime flags instead.
Best Practices
- Prioritize dependency updates over workarounds
- Check package status at pub.dev before migration
- Test thoroughly after enabling sound null safety
- Remove
--no-sound-null-safety
flags once all dependencies support null safety
Migration Checklist
- [ ] Verify all dependencies have null-safe versions
- [ ] Run
dart pub upgrade --null-safety
- [ ] Test with
--no-sound-null-safety
temporarily - [ ] Gradually migrate remaining non-null-safe code
- [ ] Remove null safety flags once fully migrated
Tip
Most popular packages now support null safety. If you encounter this error, first check for package updates rather than immediately disabling sound null safety.
For more information, see Dart's official guide on unsound null safety.