Using curly braces in a class I can use the @required parameter and this way I can have one value that is required, and one optional, as follows:

class MyClass extends StatelessWidget {
    MyClass({@required this.colour, this.other});

    final Color colour;
    final Widget other;

  }

But that means I have to call it like this:

MyClass(colour: Color(0xFF1D1E33))

Whereas I'd rather call it like this:

MyClass(Color(0xFF1D1E00))

If I remove the curly braces I can do this, but I found I cannot have a required an optional value this way:

  class MyClass extends StatelessWidget {
    MyClass(@required this.colour, this.other);

    final Color colour;
    final Widget other;

  }

It throws an error. How can I make a value optional without curly braces?


Solution 1: CopsOnRoad

You need to use positional argument denoted by []

MyClass extends StatelessWidget {
  MyClass(this.colour, [this.other]);

  final Color colour;
  final Widget other;

}

So, in order to instantiate the class you need to use

MyClass(Color(0xFF1D1E00)); // in this case other will be null

MyClass(Color(0xFF1D1E00), Container()); // other will be Container