Dart
Dart是一种面向对象的语言,具有C风格的语法,可以选择将其编译成JavaScript。它支持各种编程辅助工具,如接口,类,集合,泛型和可选类型。
Dart编程语法:
变量和运算符
标识符是给予程序中元素的名称,如变量,函数等。
标识符的规则是标识符可以包括字符和数字。但是,标识符不能以数字开头。
除下划线(_)或美元符号($)外,标识符不能包含特殊符号。
-
标识符不能是关键字。 -
它们必须是唯一的。 -
标识符区分大小写。 -
所有未初始化的变量的初始值为null标识符不能包含空格。
数据类型
-
数字
Dart中的数字用于表示数字文字。Dart有两种数字类型:
-
整数 - 整数值表示非小数值,即没有小数点的数值。 例如,值10是整数。整数文字使用 int 关键字表示。
-
浮点型 - Dart也支持小数数值,即带小数点的值。 Dart中的Double数据类型表示64位(双精度)浮点数。例如,值10.10。关键字 double 用于表示浮点文字。
parse() 静态函数允许将字符串解析为整型。 例如:num.parse(10)
如果传递除数字以外的任何值,则解析函数抛出FormatException -
-
字符串
关键字 String 用于表示字符串文字。字符串值嵌入单引号或双引号中。Dart中的字符串值可以使用 单引号 或 双引号 或 三引号 表示。单行字符串使用单引号或双引号表示。三引号用于表示多行字符串。
String name = 'value' 或者 String name = ''value'' 或者 String name = '''hello world''' 或者 String name= ''''''hello world''''''字符串插值
void main() { String str1 = "hello"; String str2 = "world"; String res = str1+str2; print("The concatenated string : ${res}"); } -
布尔
-
列表
-
动态类型
var 例如: var name = 'Smith'; val 例如: val age = 9 dynamic 静态检查时关闭类型检查,代表这段代码的编写者知道自己在干什么,不推荐使用。
循环
for循环
for
for..in
while 循环
while
do..while
判断
if
if..else
switch..case
列表
List
声明
var name = new List();
api举例:
name.add(1);
name.add("haha");
name.add(true);
name.replaceRange(0, 2, [false,2]);
Map
注意: Map值可以是包括NULL的任何对象。
函数
dart允许将函数作为参数进行传递
void main() {
printMsg();
print(test());
}
printMsg()=> print("hello");
接口
class identifier implements interface-1,interface_2,interface_3……
声明类
语法
class class_name {
<fields>
<getters/setters>
<constructors>
<functions>
}
异常处理
try {
// code that might throw an exception
}
on Exception1 {
// exception handling code
}
catch Exception2 {
// exception handling
}
finally {
// code that should always execute; irrespective of the exception
}
数据流
Dart Future是dart:async 包提供的异步请求
void main(){
File file = new File( Directory.current.path+"\\data\\contact.txt");
Future<String> f = file.readAsString();
// returns a futrue, this is Async method
f.then((data)=>print(data));
// once file is read , call back method is invoked
print("End of main");
// this get printed first, showing fileReading is non blocking or async
}