dart基本语法


Dart 版本 2.x
Dart 是一种适用于万维网的开放源代码编程语言
Google 主导开发 2011年10月公开
目标在于成为下一代结构化Web开发语言
文档参考 //dart.dev/guides/language/language-tour
// 定义一个函数
printNumber(num aNumber) {
    print('The number is $aNumber.'); // 打印内容
}
// 主程序执行入口
main() {
    var number = 42; // 定义并初始化一个变量
    printNumber(number); // 调用自定义的函数
}
// 打印结果
// The number is 42
Here’s what this program uses that applies to all (or almost all) Dart apps:
// This is a comment.
A single-line comment. Dart also supports multi-line and document comments. For details, see Comments.
int
A type. Some of the other built-in types are String, List, and bool.
42
A number literal. Number literals are a kind of compile-time constant.
print()
A handy way to display output.
'...' (or "...")
A string literal.
$variableName (or ${expression})
String interpolation: including a variable or expression’s string equivalent inside of a string literal. For more information, see Strings.
main()
The special, required, top-level function where app execution starts. For more information, see The main() function.
var
A way to declare a variable without specifying its type.
Note: This site’s code follows the conventions in the Dart style guide.
Dart 的注意事项
所有能够使用变量引用的都是对象,每个对象都是一个类的实例。
Dart 中 甚至连数字、函数和 null 都是对象,所有的对象都继承于 Object 类。
使用静态类型(例如前面示例中的 num ) 可以更清晰的表明你的意图,
并且可以让静态分析工具来分析你的代码,但这并不是强制性的(在调试代码的时候你可能注意到,没有指定类型的变量的类型为 dynamic)。
Dart 在运行之前会先解析你的代码,可以通过使用类型或者编译时常量来帮助 Dart 去捕获异常来让代码运行的更高效。
Dart 支持顶级函数 (例如 main()),同时支持在类中定义函数,如:静态函数 和 实例函数。 还可以在函数中定义再函数(嵌套函数 或 局部函数)。
Dart 支持顶级变量,及在类中定义变量(静态变量 和 实例变量)。实例变量有时候被称之为字段(Fields)或者属性(Properties)。
Dart 没有 public、protected 和 private 关键字。
标识符以 _ 开头,则该标识符在库内是私有的。
详情参考 库和可见性//dart.goodev.org/guides/language/language-tour#libraries-and-visibility
标识符 必须为字母或者下划线开头 后面 是其他字符和数字的组合。
表达式 expression 和语句 statement 是有区别的,条件表达式 condition ? expr1 : expr2 的值为 expr1 或 expr2。
将其与 if-else 语句进行比较是没有价值的。
一条语句通常包含了一个或多个表达式,但是一个表达式不能直接包含一条语句。
Dart 开发时仅提示两个等级的问题,分别是 警告 和 错误。