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?
required
instead of@required
– Antipyretic@required
withrequired
. – Diley