Flutter null safety checks - flutter

flutter seems to be switching between null safety checks and nonnull safety checks causing dozens of issues.
Running: flutter clean remove all null check errors,
but when I run: flutter pub get the null errors return?
Really confusing, my dart sdk:
environment:sdk: ">=2.11.6 <3.0.0"
I think dart was updated recently to include null checks and it seems to be causing loads of problems for me, I have corrected the null errors before anyone suggests this and when I run flutter clean the null error check I have implemented cause errors.
lib/screens/add_new_location_note_screen.dart:23:8:
Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher. Place? _pickedLocation;
Now I have changed the minimum sdk to
environment: sdk: ">=2.12.0 <3.0.0"
ran: flutter clean
and the errors again disappear now running: flutter pub get
the null errors checks return ???

You must set the SDK constraints like so:
environment: sdk: ">=2.12.0-0 <3.0.0"
Currently, the 2.12 SDK is in pre-release (beta), requiring the use of the -0 to signify the pre-release version to use.

Related

When I upgraded the dart language I get an error in doesn't support null safety from the universal_ui package

When I tried to run this transaction: flutter pub add universal_ui
I get this error:
The current Dart SDK version is 3.0.0-204.0.dev.
Because pt_proj depends on universal_ui any which doesn't support null safety, version solving failed.
The lower bound of "sdk: '>=2.7.0 <3.0.0'" must be 2.12.0 or higher to enable null safety.
For details, see https://dart.dev/null-safety
How can I solve it?

Null safety migration error: package has unmigrated dependencies. But all my dependencies declare support for null-safety

Im trying to migrate dart null safety but I get the following error when I run dart migrate
Bad state: Error: package has unmigrated dependencies.
Before migrating your package, we recommend ensuring that every library it
imports (either directly or indirectly) has been migrated to null safety, so
that you will be able to run your unit tests in sound null checking mode. You
are currently importing the following non-null-safe libraries:
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_data/basic_project.dart
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_data/deferred_components_config.dart
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_data/project.dart
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_driver.dart
file:///C:/flutter/packages/flutter_tools/test/src/test_flutter_command_runner.dart
file:///C:/flutter/packages/flutter_tools/test/src/testbed.dart
package:dwds/data/build_result.dart
package:dwds/data/connect_request.dart
package:dwds/data/debug_event.dart
package:dwds/data/devtools_request.dart
package:dwds/data/error_response.dart
package:dwds/data/extension_request.dart
package:dwds/data/isolate_events.dart
package:dwds/data/register_event.dart
package:dwds/src/debugging/dart_scope.dart
package:dwds/src/debugging/debugger.dart
package:dwds/src/debugging/execution_context.dart
package:dwds/src/debugging/frame_computer.dart
package:dwds/src/debugging/inspector.dart
package:dwds/src/debugging/instance.dart
package:dwds/src/debugging/libraries.dart
package:dwds/src/debugging/location.dart
package:dwds/src/debugging/metadata/class.dart
package:dwds/src/debugging/metadata/function.dart
package:dwds/src/debugging/metadata/module_metadata.dart
package:dwds/src/debugging/metadata/provider.dart
Please upgrade the packages containing these libraries to null safe versions
before continuing. To see what null safe package versions are available, run
the following command: `dart pub outdated --mode=null-safety`.
When I run dart pub outdated --mode=null-safety I got this message and everything seems to be ready for null safety:
C:\flutter\packages\flutter_tools>flutter pub outdated --mode=null-safety
Showing dependencies that are currently not opted in to null-safety.
[✗] indicates versions without null safety support.
[✓] indicates versions opting in to null safety.
All your dependencies declare support for null-safety.
How can I upgrade these packages and migrate to null safety? Appreciate every answer :)
You can use migration helper tool with this command line:
dart migrate
Or you can migrate manually after change min flutter sdk version in pubspec.yaml
environment:
sdk: '>=2.12.0 <3.0.0'
and after this change you need launch this command:
flutter pub get
The tool is listing the following files as non-null-safe:
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_data/basic_project.dart
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_data/deferred_components_config.dart
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_data/project.dart
file:///C:/flutter/packages/flutter_tools/test/integration.shard/test_driver.dart
file:///C:/flutter/packages/flutter_tools/test/src/test_flutter_command_runner.dart
file:///C:/flutter/packages/flutter_tools/test/src/testbed.dart
They belong to Flutter itself:
https://github.com/flutter/flutter/tree/master/packages/flutter_tools/test
So you should update Flutter itself. You can do this in pubspec.yaml:
environment:
sdk: ">=2.15.0 <3.0.0"
flutter: ">=2.10.0" # <= here
I am not sure which version got those files migrated, so try the most recent one that does not break your code too much.
That is because you need update your packages, some packages don't have support for null safety , then you need to check each one of your packages ,
for example.. you can looking some package and on the top say if the package have or not have support for null safety (for example the image ), (sometimes users update in other repositories then check on the issues page from GitHub *if the package don't have support)

'package:splashscreen/splashscreen.dart' is legacy, and should not be imported into a null safe library

how to resolve this error of null safe library in splash screen even though I have added dependency most recent one i.e "splashscreen: ^1.3.5"
To import a package without null safety support reduce your flutter version in pubspec.yaml
Replace environment with bellow version
environment:
sdk: ">=2.06.0 <3.0.0"
Note: This is one way to use an external package without null safety support. If it's an existing project check your dependencies for null safety support before migrating.

Flutter: Null safety features are disabled for this library when using InterstitialAd? _interstitialAd;

I'm integrating Interstitial ads in my flutter project.
But When I declare like this
InterstitialAd? _interstitialAd;
I am getting this error:
33:17: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
InterstitialAd? _interstitialAd;
^
lib/admob_service.dart:64:20: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
_interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
^
lib/admob_service.dart:79:20: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
_interstitialAd!.show();
I saw some stackoverflow answers and tried to upgrade the sdk version from
environment:
sdk: ">=2.7.0 <3.0.0"
to
environment:
sdk: ">=2.12.0 <3.0.0"
Then I got errors in my whole project.The error message is :
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:
- package:local_database
- package:auto_size_text
- package:queue
Is there any way other than declaring this : InterstitialAd? _interstitialAd;
Seems like the packages you mentioned, haven't been migrated to null safety hence the error.
The only workaround here is you update the packages to their null-safety version (if any) OR just don't use sound null safety for your project.

Error: This requires the 'non-nullable' experiment to be enabled

I am playing around with non-nullable types and added this to my analysis_options.yaml:
analyzer:
enable-experiment:
- non-nullable
I have a code-generator that is utilising the nullability extension. Visual Code is fine with my code.
Now, I try to run:
flutter packages pub run build_runner watch
I get this error message:
[SEVERE] Failed to snapshot build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
[SEVERE] xyz.dart:95:7: Error: This requires the 'non-nullable' experiment to be enabled.Try enabling this experiment by adding it to the command line when compiling and running.
How can I pass --enable-experiment:non-nullable to flutter packages pub run?
The same happens if I run:
flutter build ios
I get the error message:
lib/main.dart:61:26: Error: This requires the 'non-nullable' experiment to be enabled.
Try enabling this experiment by adding it to the command line when compiling and running.
So, same question: How can I pass --enable-experiment:non-nullable to flutter build?
This problem was occured for me after upgrading the Flutter.
I solved it by cleaning and upgrading the project dependencies again.
Run the below commands in the root directory of your project:
flutter clean
flutter packages pub upgrade
flutter pub run build_runner build
Also as others said, please make sure your sdk version in the pubspec.yaml is compatible with your flutter sdk version.
For null safety to work,
environment:
sdk: ">=2.12.0 <3.0.0"
should be this version at least.
then run flutter clean
and flutter pub get it will work.
Try
flutter clean
This is worked for me!
Depending on what you're doing, sometimes it's as simple as changing the environment in your pubspec.yaml file to update the lower end, ie change a lower end like
environment:
sdk: ">=2.0.0 <3.0.0"
to
environment:
sdk: ">=2.6.0 <3.0.0"
or
environment:
sdk: ">=2.7.0 <3.0.0"
This has worked for the various things that cause this error (using spread syntax in latest version of flutter and dart will also cause this), but I don't need the older environments.
A simple fix for me was to ensure that the analysis_options.yaml is in
the root folder. In my case, it was in the lib folder and none of the
experimental features worked until I moved the file to the same folder
as pubspec.yaml. Other things that can happen include updates that
make the experimental feature no longer required so updating to the
latest version on the flutter master channel may be useful in addition
to verifying the file location. Haven't been able to get the command line
running with this option, however.
Dart Reference
Example of Analysis Options File