Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

apply

Calls a function with a List of arguments as its positional parameters.

R apply<R>(Function f, List<Object?> args)

Lecture

apply exists for the moments when the arguments to a call aren't sitting in separate variables — they're already collected in a List (parsed from input, gathered from a config, produced by juxt) and you need to invoke an arbitrary function with them. Under the hood it's a thin wrapper over Dart's own Function.apply, with the result cast to R.

Because f is typed as the bare Function, apply is inherently dynamic — Dart can't check the argument count or types against f's signature at compile time, only at runtime. Reach for it only when you genuinely have a dynamic arg list; for anything else, call the function directly.

A related but separate concern is currying — pre-filling some arguments of a function ahead of time. FxTS has a fully generic curry; Dart's type system has no equivalent for arbitrary arities, so FxDart ships only a @Deprecated two-argument curry stub as a migration aid. Prefer writing the closure yourself: (b) => f(a, b).

Demo 1 · Basics

Demo 2 · Dispatching dynamic calls

A small command dispatcher, where each handler has a different arity and the arguments arrive as a runtime List:

Try it yourself

Exercise: call greet below using apply with the argument list ['Kim', 'Hello'].

Related: juxt — often the source of a dynamic arg list · tap — run a side effect without changing the value · cases — dispatch on predicates instead of a name · pipe — dynamic composition, same trade-off as apply