gegobyte
gegobyte

Reputation: 5545

Flutter: This requires the 'non-nullable' language feature to be enabled

Getting error in MyApp constructor

const MyApp({Key? key}) : super(key: key);

This requires the 'non-nullable' language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.12.0 or higher, and running 'pub get'.

but sdk is by default set to 2.12.0 already

sdk: ">=2.12.0 <3.0.0"

This project is created with the latest versions of Flutter and Dart.

Dart version: 2.13.3

Flutter version: 2.2.2

Upvotes: 6

Views: 8142

Answers (3)

bbjay
bbjay

Reputation: 1820

In my case, I had to set it to 2.13.0 to make the error go away:

sdk: ">=2.13.0 <3.0.0"

Upvotes: 4

cameron1024
cameron1024

Reputation: 10136

A few possibilities:

  • The file may have overridden the language level. You can set a specific language version for a file by putting a comment right at the top like: // @dart=2.9. This will disable null safety for that file. Check for such a comment, and remove to solve the error
  • Similarly, if the entrypoint to your app (usually lib/main.dart) has an overridden language level, it will apply that to the project. This error occurs when using flutter run, but is typically not shown in the IDE during development
  • Your IDE may be using a different version of flutter to the version that you used to capture your versions. You can check the location of your flutter install (on Linux-like systems) by running which flutter to see where on your PATH the executable is. You can then compare this to the version your IDE uses (varies by IDE)

Upvotes: 2

Rex
Rex

Reputation: 313

it is because of your IDE is using older version of flutter so try to upgrade flutter using this command in your terminal

flutter upgrade

if stills problem occurs try to clean the project by using command in terminal

flutter clean

if you want to check whether the flutter is installed properly or not you can check this by writing command in your terminal

flutter doctor

Upvotes: 1

Related Questions