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

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.

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.

How to disable Flutter sound-safety and null-safety for the SplashScreen library?

I am trying to run the SplashScreen library but it isn't working.
I have edited the package’s pubspec.yaml file, setting the minimum SDK constraint to 2.7.0,
It brought out this error:
lib/mySplashScreen.dart:5:28: 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. const MySplashScreen({Key?
key}) : super(key: key);
^ Error: Cannot run with sound null safety, because the following dependencies don't support null safety:
package:ar_app
package:splashscreen For solutions, see https://dart.dev/go/unsound-null-safety
I added a language version comment to the top of the Dart files, ie: // #dart=2.9
It brought this error:
Error: A library can't opt out of null safety by default, when using
sound null safety. // #dart=2.9 ^^^^^^^^^^^^
What do I do?
If you want you can just disable null safety, without downgrading.
Add this to your launch configuration:
--no-sound-null-safety
More info on https://dart.dev/null-safety/unsound-null-safety
it saying you can not user a non null safe package while using Dart null safety or sound null safety
Now you have one option to keep using non null safe package. that is you have to downgrade your flutter to non null safe version
to achieve that run the following command
flutter pub downgrade
To disable null safety change flutter SDK version to:
environment:
sdk: ">=2.11.0 <3.0.0"
in pubspec.yaml file

Flutter null safety checks

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.