Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Dart error: the parameter can't have a value of 'null' because of its type

ErrorThe parameter 'title' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

Short answer

An optional parameter that is not passed gets the value null. If its type is non-nullable, such as String or int, Dart refuses to compile. Pick one: make the type nullable with a question mark, give it a non-null default, or make a named parameter required.

  • Applies to Dart 3.x, Flutter 3.x stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the analyzer. The message names the parameter and the file and line of the constructor or function.

    dart analyze
  2. 2Match what you see, then jump to its fix

    1. 1The parameter is inside curly braces, for example {this.title}, and its type has no question markA named parameter has no default and is not requiredFix
    2. 2Callers sometimes have no value to send, and null should mean no valueThe value is genuinely optionalFix
    3. 3A missing value should behave like a known value, such as 0 or an empty stringA sensible default existsFix
    4. 4The argument type 'String?' can't be assigned to the parameter type 'String'A nullable value is passed to a non-nullable parameterFix

What does the error look like?

  • The parameter 'title' can't have a value of 'null' because of its type, but the implicit default value is 'null'.

Cause 1 of 4

A named parameter has no default and is not required

1Check

Named parameters are optional by default. When the caller leaves one out, it is null, which a non-nullable type cannot hold.

grep -rn "this\." lib/ | grep "{" | head

2Fix

If the caller must always send it, add required. This is the usual choice for widget constructors.

class Card extends StatelessWidget {
  const Card({super.key, required this.title});
  final String title;
  // ...
}

3Verify

The analyzer reports no issue for the file.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 2 of 4

The value is genuinely optional

1Check

Decide what a missing value means. If null is a valid answer, the type should say so.

dart analyze

2Fix

Add a question mark to the type, then handle null where the value is used, for example with a null check or ??.

void show({String? note}) {
  print(note ?? 'No note');
}

3Verify

The call without the argument compiles and prints the fallback.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 3 of 4

A sensible default exists

1Check

Look for a value that is safe whenever the caller says nothing.

dart analyze

2Fix

Write the default in the parameter list. Optional positional parameters work the same way.

void load({int retries = 3}) {}
void pad([int width = 1]) {}

3Verify

Calls with and without the argument both compile.

dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 4 of 4

A nullable value is passed to a non-nullable parameter

1Check

This is the sibling error. The parameter is fine; the value you pass may be null, for example a field or a map lookup.

dart analyze

2Fix

Test the local value before passing it. Dart narrows a local variable after a null check. A field is not narrowed, so copy it into a local variable first.

final name = user.name; // a nullable field
if (name != null) {
  greet(name);
}

3Verify

The analyzer accepts the call inside the if block.

dart analyze

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Write required on named constructor parameters unless a missing value has a clear meaning.
  • Make a type nullable only when null is a real state in your data.
  • Run dart analyze in CI so the error never reaches a release branch.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.