Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The parameter can't have a value of 'null' because of its type in Dart

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?

like image 931
creativecreatorormaybenot Avatar asked Oct 27 '20 18:10

creativecreatorormaybenot


People also ask

What is the type of null in Dart?

The reserved word null denotes an object that is the sole instance of this class. The Null class is the only class which does not implement Object . It is a compile-time error for a class to attempt to extend or implement Null.

Can't have a value of null because of its type?

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() .

How do you assign a null value in Dart?

When creating { question: 'How old are you?' , answer: null } try specify the precise type you want like <String, dynamic>{ question: 'How old are you?' , answer: null } .


2 Answers

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) {} // bar(null) is invalid.  void bar(int? param1) {} // bar(null) is valid. 
like image 181
creativecreatorormaybenot Avatar answered Sep 22 '22 18:09

creativecreatorormaybenot


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); 
like image 34
iDecode Avatar answered Sep 20 '22 18:09

iDecode