Diff between positional and Named params in func ? example :- `func({required CancelToken cancelToken})` VS `func(CancelToken cancelToken)`

Answer

Overview

Dart functions support two types of parameters: positional (order-based) and named (explicitly identified by name). Understanding the difference is important for writing clean, readable APIs.


Positional Parameters

Arguments are passed in order. The caller must provide them in the exact sequence defined.

dart
// Function definition
String greet(String name, int age) {
  return 'Hello $name, you are $age years old';
}

// Calling — order matters!
greet('Alice', 28);       // ✅ Correct
greet(28, 'Alice');       // ❌ Error — wrong order

Optional Positional Parameters (with
text
[]
)

dart
String greet(String name, [String title = 'Mr/Ms']) {
  return 'Hello $title $name';
}

greet('Alice');            // Hello Mr/Ms Alice
greet('Alice', 'Dr');      // Hello Dr Alice

Named Parameters

Arguments are passed by name — order does not matter. Defined with

text
{}
.

dart
// Function definition
String greet({required String name, required int age}) {
  return 'Hello $name, you are $age years old';
}

// Calling — order doesn't matter!
greet(name: 'Alice', age: 28);  // ✅
greet(age: 28, name: 'Alice');  // ✅ Same result

Optional Named Parameters (with default values)

dart
void makeRequest({
  required String url,
  String method = 'GET',
  int timeout = 30,
  Map<String, String>? headers,
}) {
  print('$method $url (timeout: ${timeout}s)');
}

// Only required param needed
makeRequest(url: 'https://api.example.com/users');

// All params
makeRequest(
  url: 'https://api.example.com/users
  method: 'POST',
  timeout: 60,
  headers: {'Authorization': 'Bearer token'},
);

The Example from the Question

dart
// Named parameter (explicit name required)
Future<void> cancelRequest({required CancelToken cancelToken}) async {
  cancelToken.cancel();
}

// Calling
cancelRequest(cancelToken: myToken); // ✅ Name required

// ─────────────────────────────────────────

// Positional parameter (just pass the value)
Future<void> cancelRequest(CancelToken cancelToken) async {
  cancelToken.cancel();
}

// Calling
cancelRequest(myToken); // ✅ No name needed

Key Differences

FeaturePositionalNamed
Call syntax
text
func(value)
text
func(param: value)
Order matters✅ Yes❌ No
Self-documenting❌ Less clear✅ Very clear
Optional support
text
[param]
text
{param}
Required keywordNot needed
text
required
keyword
Best forSimple, obvious paramsMultiple or optional params

Best Practices

dart
// ✅ Good — named params for clarity when multiple params
Widget buildCard({
  required String title,
  required String subtitle,
  Color color = Colors.white,
  VoidCallback? onTap,
}) { ... }

// ❌ Avoid — positional gets confusing with many params
Widget buildCard(String title, String subtitle, Color color, VoidCallback? onTap) { ... }
// buildCard('Title', 'Subtitle', Colors.red, null) — hard to read!

Rule: Use positional for 1–2 obvious parameters. Use named parameters with

text
required
when a function has 3+ parameters or when the parameter's purpose isn't obvious from its type alone.