flutter_in_action_2nd icon indicating copy to clipboard operation
flutter_in_action_2nd copied to clipboard

1.4.2.1 函数返回值没有类型推断错误

Open scottfly189 opened this issue 1 year ago • 1 comments

1.4.2.1 Dart函数声明如果没有显式声明返回值类型时会默认当做dynamic处理,注意,函数返回值没有类型推断

》》实际我敲代码,如果没有显示声明返回值类型,是不会当做dynamic处理,都有正确的类型推断。

class Test {}

main() {
  print(getTest1().runtimeType);
  print(getTest2().runtimeType);
  print(getTest().runtimeType);
}

getTest1() {
  return 123;
}

getTest2() {
  return "hello";
}

getTest() {
  return new Test();
}

返回值结果如下: int String Test

scottfly189 avatar Aug 23 '24 00:08 scottfly189