Differentiate between required and optional parameters in Dart

Technology CommunityCategory: FlutterDifferentiate between required and optional parameters in Dart
VietMX Staff asked 3 years ago

Required Parameters

Dart required parameters are the arguments that are passed to a function and the function or method required all those parameters to complete its code block.

findVolume(int length, int breath, int height) {
 print('length = $length, breath = $breath, height = $height');
}

findVolume(10,20,30);

Optional Parameters

  • Optional parameters are defined at the end of the parameter list, after any required parameters.
  • In Flutter/Dart, there are 3 types of optional parameters: – Named – Parameters wrapped by { } – eg. getUrl(int color, [int favNum]) – Positional – Parameters wrapped by [ ]) – eg. getUrl(int color, {int favNum}) – Default – Assigning a default value to a parameter. – eg. getUrl(int color, [int favNum = 6])