Dart Lambda / Shortwave Confusion

I'm still pretty new to Dart, and the syntax => (bold arrow) still confuses me (I'm from C # background).

So in C # the bold arrow (=>) says: goes to like this for example:

Action<string> action1 = (str) => { System.Diagnostic.Debug.WriteLine("Parameter received: " + str.ToString()); }

action1("Some parameter");

means: whatever the action1quality of the parameter in action1(if it can be converted to string), it falls into the inner region (in our case, it simply prints inDebug.WriteLine()

but in darts it's something else .... (?)

for example, in Future.then

ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then( 
   (str) => { print("Class was loaded with info: $str"),
   onError: (exp) => { print("Error occurred in class loading. Error is: $exp"); }
);

Editor darts warns me that the first and second print: Expected string literal for map entry key. I think in C # so that strit's just a name for a parameter that will be populated with an internal callback that is Future.thenused to call onValueoronError

What am I doing wrong?

+32
source
3

, .

= > {}

, :

ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then( 
  (str) => print("Class was loaded with info: $str"),
  onErrro: (exp) => print("Error occurred in class loading. Error is: $exp")
);

ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then( 
  (str) { print("Class was loaded with info: $str"); },
  onErrro: (exp) { print("Error occurred in class loading. Error is: $exp"); }
);

.

, , = > . :

someFunction.then( (String str) => print(str) );

, , .

someFunction.then( (String str) {
  str = str + "Hello World";
  print(str);
});

, 2- , .

, .

+48

In Dart => xxx - , { return xxx; }. , :

var a = (String s) => s;
var b = (String s) { return s; } ;

=> :

String myFunc(String s) => s;
String myFunc(String s) {
  return s;
}
+28

This syntax works well in languages ​​such as javascript, as well as C #, where it supports (param1, param2, …, paramN) => { statements }where the operator is split (param1, param2, …, paramN) => { statements }. In a dart, a bold arrow only supports expression, which is short for. { return expr; } { return expr; }
This explains your mistake. Your curly bracket code (exp) => { print("Error occurred in class loading. Error is: $exp"); } (exp) => { print("Error occurred in class loading. Error is: $exp"); }means you are returning a map, so it expects to see something like (param) => {"key": "value"}where key is a string literal.

0
source

All Articles