Named Constructor with required parameters and null safety

Viewed 4485

The following code does not compile with sound null safety because it is possible to pass null to the constructor which initializes a non-nullable field myString.

class MyClass {
  String myString;
  MyClass({@required this.myString});
}

I would expect that adding a question mark after this.myString would solve the problem, but it doesnt and another compile error is shown.

A default value is no option for me and it seems that the only other solution is something like this:

class MyClass {
  late String myString;
  MyClass({@required myString}) {
    this.myString = myString;
  }
}

Imo this decreases readability if there are a lot of parameters. Is there a more convenient solution which suppports initialization with this. in named constructors?

2 Answers

For non-nullable types, either use required or provide a default value. You can also use late but make sure to provide a value at some later point before using it. It's like a bang operator.

Example:

class Foo {
  // All are non-nullable.
  final int a;
  final int b;
  late final int c; // I trust you that you'll provide me a value later. 
  
  Foo({
    required this.a, // <-- Mark it required  
    this.b = 0, // <-- or provide a default value. 
  });
}

If i am not getting the idea wrong, if you want to have a String param which is nullable from the constructor, you can declare a 'String?' type.

class MyClass {
  String? myString;
  MyClass({required this.myString});
}
Related