131

Dart function

I have the following Dart function and I am now using null safety:

void calculate({int factor}) {
  // ...
}

The analyzer complains that:

The parameter 'factor' can't have a value of 'null' because of its type, and no non-null default value is provided.

Flutter widget

This is also the case for my StatelessWidget in Flutter:

class Foo extends StatelessWidget {
  const Foo({Key key}): super(key: key);

  // ...
}

I get the following error:

The parameter 'key' can't have a value of 'null' because of its type, and no non-null default value is provided.


How can I resolve this issue?

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402

8 Answers8

223

Why

The reason this happens is because with null safety enabled, your non-nullable parameter factor or key cannot be null.

In the function and the constructor, these values might be null when the function is called without the named parameter: calculate() or Foo(). However because the types (int and Key) are non-nullable, this is invalid code - they must never be null.

Solutions

There are essentially three ways of solving this:

required

This is probably the most common solution to this problem and it indicates that a variable has to be set. This means that if we have (notice the required keyword):

void calculate({required int factor}) {
  // ...
}

We indicate that the factor parameter must always be specified, which solves the problem because only calculate(factor: 42) et al. will be valid calls of the function.

Default value

Another solution is providing a default value. If our parameter has a default value, we can safely not specify the parameter when calling the function because the default value will be used instead:

void calculate({int factor = 42}) {
  // ...
}

Now, a calculate() call will use 42 as the factor, which is obviously non-null.

Nullable parameter

The third solution is something that you really want to consider, i.e. do you want to have a nullable parameter? If so, you will have to null check the parameter when using it in your function.

However, it is the way you would most commonly want to solve the Key key issue because you do not always want to provide a key to your widget in Flutter (note the nullable Key? type):

class Foo extends StatelessWidget {
  const Foo({Key? key}): super(key: key);

  // ...
}

Now, you can safely construct Foo() without providing a key.

Positional parameters

Note that the same applies to positional parameters, i.e. they can be made nullable or non-nullable, however, they cannot be annotated with required and cannot have default values as they are always required to be passed.

void foo(int param1) {} // foo(null) is invalid.

void bar(int? param1) {} // bar(null) is valid.
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • class SectionListTileWidget extends StatefulWidget { final BuildContext tileContext; final IconData tileLeadingIcon; final Color tileLeadingIconColor; final String tileTitle; final Function tileOnTapAction; SectionListTileWidget( {required this.tileContext, required this.tileLeadingIcon, required this.tileLeadingIconColor, required this.tileTitle, required this.tileOnTapAction}); @override _SectionListTileWidgetState createState() => _SectionListTileWidgetState(); } – Kamlesh May 16 '21 at 17:34
  • how can I make "tileLeadingIconColor" nullable so that if I have no tileLeadingIconColor (null) then I can use Colors.green otherwise want to use user's passed tileLeadingIconColor. Kindly suggest how can i do this. Thanks a lot. – Kamlesh May 16 '21 at 17:35
  • I agree with you, this is optional choice : use `required int param` or `int? param` – Huy Tower Jul 24 '21 at 03:09
  • 1
    Thanks a lot for the clear answer, but I want to know when the non-nullable feature was allowed in the previous Dart version and in wish one. – Salah dine Maham Feb 16 '22 at 17:58
23

It's the main reason why non-nullable feature is added to Dart. Since, you're passing Key to super class, which could be null, so you want to make sure it's non null. What you can do is either not use the key at all or provide a default value to it or make it required. Like:

MyPage({Key key = const Key("any_key")}) : super(key: key);

or make key required like this:

MyPage({required Key key}) : super(key: key);
iDecode
  • 22,623
  • 19
  • 99
  • 186
  • Sorry for jumping in. Compared to the answer above (which told us that it is actually possible to make it 'nullable' other than 'required'), what is actually the specific case that you want this to be not empty rather than it can be empty? – Dean Debrio Jul 27 '23 at 14:22
  • @DeanDebrio I can't think of a good use case top of my head except when you are you using `AnimatedSwitcher` to switch between two widgets which require a key. – iDecode Jul 27 '23 at 17:23
14

If i get this error from the constructor of a class pointing key, i add a '?' mark infront of Key like this:

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

'?' means that can be nullable

Bensal
  • 3,316
  • 1
  • 23
  • 35
12

Add requuired function for Eg

required Key key,
    required this.id,
    required this.name,  
    required this.code,  
    required this.img,  
    required this.price,
    required this.promotionPrice,
    required this.size,
    required this.color,
bikerhunt
  • 129
  • 1
  • 2
9

As additional information to the previous @creativecreatorormaybenot's answer, you can also use positional parameters (no curly brackets) that are mandatory by default, so not nullable.

  void calculate(int factor) {
     // ...
  }

and is called without naming the parameter:

calculate(12);

These kind of parameters can be used on constructors this way:

class Foo extends StatelessWidget {
  final String myVar;

  const Foo(this.myVar, {Key? key}): super(key: key);

  // ...
}

and "can be followed either by named parameters OR by optional positional parameters (but not both)", see doc here: dart parameters

Interesting answer on difference between named and positional parameters: What is the difference between named and positional parameters in Dart?

olibiaz
  • 2,551
  • 4
  • 29
  • 31
4

Add a required keyword before the variable in the constructor and also add '?' next to Key.

  MyHomePage({Key? key, required this.title}) : super(key: key);
Luuklag
  • 3,897
  • 11
  • 38
  • 57
iamJohnvesly
  • 391
  • 3
  • 5
1

change sdk version in pubspec

environment:
  sdk: ">=2.7.0 <3.0.0"
abdul moiz
  • 21
  • 2
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Cristik Apr 17 '22 at 17:09
0

Changing the SDK version in pubspec.yaml solves the issues:

environment: sdk: ">=2.1.0 <3.0.0"

  • Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since [another user already posted that solution](https://stackoverflow.com/a/71903177/2828341). If a previous answer was helpful to you, you should [vote it up](https://stackoverflow.com/help/privileges/vote-up). – lepsch Jun 24 '22 at 23:28