Put optional positional parameters in [ ] and named parameters in { }. Give them a default value or make them nullable, and mark named ones required when they must be passed.
// Optional positional
String greet(String name, [String greeting = 'Hello']) => '$greeting, $name';
greet('Ada'); // Hello, Ada
greet('Ada', 'Welcome'); // Welcome, Ada
// Named (the usual choice for widgets)
class Badge extends StatelessWidget {
const Badge({super.key, required this.label, this.color, this.size = 16});
final String label;
final Color? color; // optional, nullable
final double size; // optional, with a default
@override
Widget build(BuildContext context) => Text(label);
}
Badge(label: 'New', size: 20);Named parameters can be passed in any order and make calls self-describing. A parameter that is neither required nor given a default must be nullable, and you cannot mix optional positional and named parameters in the same function.
