pass map as argument into a function for named argument

1,311 views
Skip to first unread message

Degenerate Tech

unread,
Sep 7, 2020, 3:11:21 AM9/7/20
to Dart Misc
//i want to pass a map in to function ..to give values into named variable ... but i got error 

f({var name="",var work=""}){
  print(name);
  print(work);
}


void main() {

 var work,name;
  var m={ name:"sahil", work:"play"};
  f(m);

}

Erik Ernst

unread,
Sep 7, 2020, 4:16:08 AM9/7/20
to Dart Misc
You cannot pass named parameters to a function by passing a `Map` in a regular invocation, but `Function.apply` is a special feature that allows you to pass a `Map<Symbol, dynamic>` of named arguments and get a similar effect:

f({name = "", work = ""}) {
  print(name);
  print(work);
}

void main() {
  var m = {#name: "sahil", #work: "play"};
  Function.apply(f,[],m);
}

It's worth noting that Dart is moving in the direction of more strict static checking, and this feature is inherently out of reach for static type checking.

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/7da9842a-8538-44b6-9d87-3490869aa72ao%40dartlang.org.


--
Erik Ernst  -  Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 København K, Denmark
CVR no. 28866984

Reply all
Reply to author
Forward
0 new messages