apply method - Function class - dart:core library (original) (raw)

apply static method

dynamic apply(

  1. Function function,
  2. List? positionalArguments, [
  3. Map<Symbol, dynamic>? namedArguments ])

Dynamically call function with the specified arguments.

Acts the same as dynamically calling function with positional arguments corresponding to the elements of positionalArgumentsand named arguments corresponding to the elements of namedArguments.

This includes giving the same errors if functionexpects different parameters.

Example:

void printWineDetails(int vintage, {String? country, String? name}) {
  print('Name: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo separator="true">,</mo><mi>C</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>t</mi><mi>r</mi><mi>y</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">name, Country: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">C</span><span class="mord mathnormal">o</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.03588em;">ry</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>country, Vintage: $vintage');
}

void main() {
  Function.apply(
      printWineDetails, [2018], {#country: 'USA', #name: 'Dominus Estate'});
}

// Output of the example is:
// Name: Dominus Estate, Country: USA, Vintage: 2018

If positionalArguments is null, it's considered an empty list. If namedArguments is omitted or null, it is considered an empty map.

void helloWorld() {
  print('Hello world!');
}

void main() {
  Function.apply(helloWorld, null);
}
// Output of the example is:
// Hello world!

Implementation

external static apply(
  Function function,
  List<dynamic>? positionalArguments, [
  Map<Symbol, dynamic>? namedArguments,
]);