Dart

作者: CaptainRoy | 来源:发表于2019-10-08 09:22 被阅读0次
    • 打印
    print('Hello World');
    
    • 变量
    String name = 'roy';
    int age = 18;
    double height = 1.88;
    print('${name},${age},${height}');
    

    变量不能更改类型,使用dynamic声明则可以改变类型

    var name = 'roy';
    name = 'WL';
    print(name);
    
    dynamic name = 'Roy';
    name = 18;
    print(name);
    
    • 获取变量类型
    var name = 'Roy';
    print(name.runtimeType);
    
    • 常量
    final country = "中国";
    const province = "湖北";
    
    • 字符串转数字
    var one = int.parse("111");
    var two = double.parse("12.22");
    print("${one} , ${one.runtimeType}");
    print("${two} , ${two.runtimeType}");
    
    • 数字转字符串
    var one = 123;
    var two = 123.123;
    var oneStr = one.toString();
    var twoStr = two.toString();
    print("${oneStr} , ${oneStr.runtimeType}");
    print("${twoStr} , ${twoStr.runtimeType}");
    
    • bool
    var isFlag = false;
    print("${isFlag} , ${isFlag.runtimeType}");
    
    if (isFlag == true) {
        print("是真");
    }
    
    • List
    var letters = ['a','b','c','d'];
    print("${letters} , ${letters.runtimeType}");
    
    List<int> numbers = [1,2,3,4];
    print("${numbers} , ${numbers.runtimeType}");
    
    • Set
    var letterSet = {'a','b','c','d'};
    print("${letterSet} , ${letterSet.runtimeType}");
    
    Set<int> numberSet = {1,2,3,4};
    print("${numberSet} , ${numberSet.runtimeType}");
    
    • Map
    var info = {'name' : 'roy', 'age' : 18};
    print("${info} , ${info.runtimeType}");
    
    Map<String,Object> infoMap = {'height' : 1.89 , 'address' : '北京市'};
    print("${infoMap} , ${infoMap.runtimeType}");
    
    • 除法 , 整除, 取余
    var num = 7;
    print(num / 3); // 除法 2.3333333333333335
    print(num ~/ 3); // 整除 2
    print(num % 3); // 取余 1
    
    • ??= 和 ??
      var name1 = "roy";
      var name2 = null;
      name1 ??= "lily";
      print(name1);
      print(name2);
    
      var temp = "why";
      temp = null;
      var tempName = temp ?? "kobe";
      print(tempName);
    
    • 循环
    for (var i = 0; i < 5; i++) {
        print(i);
    }
    
    var names = ["roy","lily","leo"];
    for (var name in names) {
        print(name);
    }
    
    • switch - case
    var direction = "east";
      switch (direction) {
        case "east":
          print("东面");
          break;
        case "south":
          print("南面");
          break;
        case "west":
          print("西面");
          break;
        case "north":
          print("北面");
          break;
        default:
          print("其他方向");
      }
    

    相关文章

      网友评论

          本文标题:Dart

          本文链接:https://www.haomeiwen.com/subject/lkpguctx.html