Is there a way in Dart to pack/unpack arguments in a function (like Python for example) ?
For packing example, being able to declare a function like this :
packArguments(*listOfArguments, **mapOfArguments) {
listOfArguments.forEach((arg) => print(arg));
mapOfArguments.forEach((key, val) => print("$key => $val"));
}
And then doing this :
packArguments("I", "Put", "whatever", "I", "want, arg1: "A", arg2: 1);
Would display :
I
Put
whatever
I
want
arg1 => A
arg2 => 1
As for unpacking, being able to do something like that :
functionWithLotOfArgument(a, b, c, d, e, {aa, bb = null, cc = null}) {
// do stuff
}
var argList = [1, 2, 3, 4, 5];
var argMap = {"aa": "haha", bb: "baby"};
functionWithLotOfArgument(*argList, **argMap);
Related issue https://github.com/dart-lang/sdk/issues/29087
This is not currently supported, but you can very easily pack yourself, by passing a list & map:
void packArguments(List listOfArguments, Map mapOfArguments) {
listOfArguments.forEach((arg) => print(arg));
mapOfArguments.forEach((key, val) => print("$key => $val"));
}
void main() {
packArguments(['a', 3], {'arg1': 'a', 'arg2': 5});
}
https://dartpad.dartlang.org/98ed3a3b07a2cca049cde69ca50ca269
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With