Flutter之基本数据类型测试

1、Flutter的数据基本类型

Dart语言里一切皆为对象,所以如果没有将变初始化,那么它的默认值为null

  • Number(int、doubkle)
  • String
  • Boolean(bool)
  • List
  • Map

2、测试代码

 void testData() {

    //Number包含了int和double
    int a = 4;
    int b = 8;
    print(a + b);
    int a1;
    if (a == null) {
      print('a == null');
    } else {
      print('a != null');
    }

    if (a1 == null) {
      print('a1 == null');
    } else {
      print('a1 != null');
    }

    double c = 5.9;
    double d = 6.4;
    print(c + d);

    //String类型
    var chen = 'chen';
    var yu = 'yu';
    var name = chen + yu;
    print(name);

    var hello = '''
    hello word
    public static void main1
    ''';
    print(hello);

    var word = """
    hello word
    public stati void main2
    """;
    print(word);

    //Boolean类型
    bool isSelect = false;
    if (isSelect) {
      print('isSelect is true');
    } else {
      print('isSelect is false');
    }

    //List类型
    var list = [];
    list.add(1);
    list.add(2);
    print(list);
    print('size is ${list.length}');

    list.removeAt(0);
    print(list);
    print('size is ${list.length}');

    //Map类型
    var week = {'one':'test1', 'two':'test2'};
    print(week);
    print('week length is ${week.length}');
    week.putIfAbsent('three', () => 'test3');
    print(week);
    print('week length is ${week.length}');
  }

3、运行结果

I/flutter (24359): 12
I/flutter (24359): a != null
I/flutter (24359): a1 == null
I/flutter (24359): 12.3
I/flutter (24359): chenyu
I/flutter (24359):     hello word
I/flutter (24359):     public static void main1
I/flutter (24359):
I/flutter (24359):     hello word
I/flutter (24359):     public stati void main2
I/flutter (24359):
I/flutter (24359): isSelect is false
I/flutter (24359): [1, 2]
I/flutter (24359): size is 2
I/flutter (24359): [2]
I/flutter (24359): size is 1
I/flutter (24359): {one: test1, two: test2}
I/flutter (24359): week length is 2
I/flutter (24359): {one: test1, two: test2, three: test3}
I/flutter (24359): week length is 3

公众号:记得关注我。

(0)

相关推荐