我对 Dart 还是很陌生,=>(胖箭头)的语法仍然让我感到困惑(我来自 C# 背景)。
因此,在 C# 中,胖箭头 ( => ) 表示:例如:
Action<string> action1 = (str) => { System.Diagnostic.Debug.WriteLine("Parameter received: " + str.ToString()); }
action1("Some parameter");
意味着:任何作为参数发送到action1(如果可以转换为string)进入 内部范围(在我们的例子中它只是打印在Debug.WriteLine()
但在 Dart 中有所不同.... (?)
例如在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"); }
);
Dart 编辑器警告我,第一个和第二个print是:Expected string literal for map entry key. 我认为在 C# 方式中,str它只是为参数命名,该参数将由Future.then用于调用onValue或的内部回调填充onError
我做错了什么?