Dart lambda/shortland function confusion

You need to choose either block syntax or single expression syntax, but not both.

You can’t combine => with {}.

Your two options are as follows using your example:

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")
);

or

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"); }
);

In both cases, it is just a way to express an anonymous function.

Normally if you want to just run a single expression, you use the => syntax for cleaner and more to the point code. Example:

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

or you can use a block syntax with curly braces to do more work, or a single expression.

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

but you can’t combine them since then you are making 2 function creation syntaxes and it breaks.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)