Dart 介绍 官网 Dart 是由谷歌开发的计算机编程语言。被用于 Web、服务器、移动应用和物联网等领域的开发。它使用宽松开源许可证(BSD 证书)。Dart 被设计为面向对象的、类定义的、单继承的语言。它的语法有点类似 C 语言,并且可以转译为 JavaScript,支持混入(mixins)、接口(interfaces)、抽象类(abstract classes)、具体化泛型(reified generics)、可选类型(optional typing)与 sound type system。
Dart数据类型 数值 Number int double 字符串 String 布尔值 bool 数组 List 字典 Map Set Set 是一个元素唯一且无序的集合 Runes Symbol Null Dart 支持类型推断,所以大多数情况下,并不需要显式的指定类型。
1 2 3 4 5 6 7 main() { var name = "this is a name" ; var year = 2020 ; var money = 15.32 ; var fruits = ['apple' , 'banana' , 'lemon' , 'grape' , 'orange' ]; var student = {'name' : "Jim" , 'age' : 12 }; }
Object 是所有 Dart 对象的基类型,所以任何值都可以赋值给 Object。
1 2 3 4 5 6 7 8 9 10 11 main() { var name = "this is a name" ; var year = 2020 ; Object obj; obj = name; print (obj); obj = year; print (obj); }
数值 1 2 3 int intvalue = 0 ; double num = 1.2222 ;
字符串 1 2 3 4 5 6 7 8 9 10 11 12 String str1 = 'hello' ;String str2 = 'dart!' ;print ("$str1 $str2 " ); String str3 = ''' 第一行 第二行 第三行 ''' ;print (str3);
布尔值 1 2 3 bool booll = true ;bool bool2 = false ;print (booll);
数组 声明 1 2 3 List list1 = [];var list2 = [];List <String > list3 = ['test' , 'str' , 'arr' ];
取值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 List <String > list = ['test' , 'str' , 'arr' ]; print (list[0 ]); var listTake = [1 , 2 , 3 , 4 , 5 ];print (listTake.take(2 )); print (listTake.takeWhile((value) => value < 5 )); listTake.retainWhere((element) => element>2 ); print (listTake);
长度 1 2 List <String > list = ['test' , 'str' , 'arr' ]; print (list.length);
添加 1 2 3 4 5 List list = [];list.add(1 ); list.addAll([2 , 3 ]);
插值 1 2 3 4 5 6 7 8 List list = [];list.insert(0 , 12 ); print (list); list.insertAll(0 , [10 , 11 ]); print (list);
替换 1 2 3 4 5 6 7 8 9 10 List list = [0 , 1 , 2 ];list.setAll(1 , ['set' ,'set1' ]); print (list); List listFill = [1 , 2 , 3 , 4 , 5 ];listFill.fillRange(1 , 3 , 66 ); print (listFill);
删除 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 List listRemove = <int >[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 0 ];listRemove.remove(0 ); print (listRemove); listRemove.removeAt(0 ); print (listRemove); listRemove.removeLast(); print (listRemove); listRemove.removeRange(1 , 3 ); print (listRemove); listRemove.removeWhere((e) => e == 8 ); print (listRemove); listRemove.removeWhere((e) => e > 4 ); print (listRemove);
索引 1 2 3 List listIndex = <int >[0 , 1 , 2 , 0 ];print (listIndex.indexOf(1 )); print (listIndex.lastIndexOf(0 ));
翻转 1 2 3 List listReversed = <String >['test' , 'str' , 'arr' ];print (listReversed.reversed); print (listReversed.reversed.toList());
首尾值 1 2 3 List list = <String >['test' , 'str' , 'arr' ];print (list.first); print (list.last);
循环 1 2 3 4 5 6 7 8 9 10 11 12 13 List <String > lists = ['str' , 'str1' , 'str2' ];print (lists.map((e) => e)); print (lists.map((e) => e == 'str' )); print (lists.map((e) => e).toList()); var testList = <int >[0 , 1 , 2 , 3 , 4 , 5 , 6 ];testList.forEach(print ); print (testList.firstWhere((e) => e > 3 )); print (testList.lastWhere((e) => e<6 ));
排序 1 2 3 List listSort = [1 ,4 ,2 ,6 ,0 ];listSort.sort(); print (listSort);
乱序 1 2 3 4 List listShuffle = [0 , 1 , 2 , 3 ];listShuffle.shuffle(); print (listShuffle);
判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 final testList = <int >[0 , 1 , 2 , 3 , 4 , 5 ];print (testList.contains(0 )); print (testList.contains(1 )); print (testList.contains(6 )); print (testList.where((element) => element > 3 )); print (testList.any((element) => element > 3 )); print (testList.any((element) => element > 6 )); print (testList.every((element) => element > 3 )); print (testList.every((element) => element < 6 ));