Flutter开发 2.Dart语言基础-数据类型

Flutter2 Dart语言基础

1. Dart基本关键字

在这里插入图片描述
语言规范

2. 创建测试文件

我在本地创建了一个flutter_workspace文件夹,创建一个test.dart文件,做为学习dart语言用,还有一种方式是基于在线的DartPad来学习,大部分功能也是可以学习测试的。
DartPad

在这里插入图片描述

3. 检测是否开启空安全

void main() {
  int? v4 = null;
}

如果该代码会报如下错误,则说明当前没有使用最新版本的SDK

You can enable the experiment using the '--enable-experiment=non-nullable'

此刻可以通过升级一下Flutter来尝试修复,CMD+SHIFT+P输入Flutter(可能之前用过旧版本)
在这里插入图片描述

4. Dart 数据类型

4.1 类型说明

Dart中所有类型都是Class 、没有所谓的基本数据类型。

4.2 常量使用

4.2.1 常量定义和初始化

Dart中常量的定义可以使用const和final, const是编译时常量,类似于宏定义(#define xxx mmm)一样, final是运行时的常量,只能赋值一次不能修改。

  const int type1 = 1;
  final int type2 = 2;

声明类成员变量时,const 变量必须同时被声明为 static 的,final 变量则不需要. 在全局文件中不能使用static 来定义变量或常量.

class Moon {
  static const int type1 = 1;
  final int type2 = 2;
}

声明类成员变量时, final 变量可以在初始化列表中初始化, const变量不可以。

class Moon {
  final int type2;
  Moon() : type2=2 {
  }
}

4.2.2 const做为修饰值

const除了可以用做定义一个常量外,还可以用来修饰值,表示不可以被改变的值。

  var type1  = const [1,2,3,4,5];
  type[1]=5; //Unsupported operation: Cannot modify an unmodifiable list

在修改数组时会提示错误。

4.3 数值类型

Dart中数值类型分为两种int和double

4.3.1 显示类型直接定义

  int v1 = 1;
  double v2 = 1.1;
  print("v1=$v1, v2=$v2");
  print("v1 type is " +
      v1.runtimeType.toString() +
      ", v2 type is " +
      v2.runtimeType.toString());

在一个字符串中,以 ${表达式} 的形式引用一个变量或表达式
运行后输出结果:

v1=1, v2=1.1
v1 type is int, v2 type is double

4.3.2 使用var来定义

使用var来定义的变量,runtime来根据所赋的值来推导变量的类型,一旦确定后不可变更类型

 var v11 = 10;
 var v12 = 30.3;
 print("v11=$v11, v12=$v12");

 print("v11 type is " +
     v11.runtimeType.toString() +
      ", v12 type is " +
     v12.runtimeType.toString());

运行后输出结果:

v11=10, v12=30.3
v11 type is int, v12 type is double

如果给变量赋一个整型后,再赋一个浮点数时编译器会报错
在这里插入图片描述
错语提示为:A value of type ‘double’ can’t be assigned to a variable of type ‘int’.

4.3.3 其它赋值方式

  var v21 = 0x00FF;
  var v22 = 3e2;
  var v23 = 1.23e4;
  var v24 = 1 << 4;

  print("v21=$v21, v22=$v22, v23=$v23, v24=$v24");
  print("v21 type is " +
      v21.runtimeType.toString() +
      ", v22 type is " +
      v22.runtimeType.toString() +
      ", v23 type is " +
      v23.runtimeType.toString() +
      ", v24 type is " +
      v24.runtimeType.toString());

运行后输出结果:

v21=255, v22=300.0, v23=12300.0, v24=16
v21 type is int, v22 type is double, v23 type is double, v24 type is int

可以看到当使用e来赋值时,var定义的变量自动转为double类型

4.4 字符串类型

4.4.1 字符串定义

Dart中字符串的定义也可以使用String和var两种方式来用,为变量赋值一个字符串可以用单引号,也可以用双引号,功能上没有区别,但字符串内部使用单引号或双引号时可以省去转义字符的麻烦.

 String s1 = "start:";
  var s2 = "abc";
  var s3 = 'def';
  var s4 = '"g"hi';
  var s5 = "'j'kl";

  print("s1 type is " +
      s1.runtimeType.toString() +
      ", s2 type is " +
      s2.runtimeType.toString() +
      ", s3 type is " +
      s3.runtimeType.toString() +
      ", s4 type is " +
      s4.runtimeType.toString() +
      ", s5 type is " +
      s5.runtimeType.toString());

  print("$s1$s2$s3$s4$s5");

运行后输出结果:

s1 type is String, s2 type is String, s3 type is String, s4 type is String, s5 type is String
start:abcdef"g"hi'j'kl

4.4.2 多行字符串

当有多行字符串定义和内部使用单双引号的需求时,可以使用三个单引号或三个双引号来定义。

  var s6 = '''i'm a boss.
who are you?
   ''';
  print("$s6");

  var s7 = """ 
我叫"爪爪"
I'm zz.
  """;
  print("$s7");
i'm a boss.
who are you?
   
我叫"爪爪"
I'm zz.

4.4.3 字符串常用方法

 var s8 = "ab";
  print("repeat: " + s8 * 3); //字符串重复三次
  print("compare: ${s8.compareTo("ab")}"); //字符串相等返回 0
  print("padright: " + s8.padRight(5, "z")); //使用字符z, 补齐长度达到5
  print("find: ${s8.indexOf("b")}"); //查找字符串b 返回位置
  print("contains b: ${s8.contains("b")}"); //判断是否包含某一个字符串

运行后输出结果:

repeat: ababab
compare: 0
padright: abzzz
find: 1
contains b: true

4.4.4 raw字符串

raw字符串不会对内容中的任何转义和变量的引用做特殊处理,全部原样输出。

  var s8 = "ab";
  var s9 = "this is a code sentence \nlook here! \n$s8";
  print(s9);
  var sr9 = r"this is a code sentence \nlook here! \n$s8";
  print(sr9);

运行后输出结果:

this is a code sentence 
look here! 
ab
this is a code sentence \nlook here! \n$s8

4.5 List类型

4.5.1 基本初始化方式

 List<int> l1 = [1, 2, 3];
 var l2 = [4, 5, 6];
 var l3 = [l1, l2]; //该方式会将l3变成一个二维list
 print("""
  l1=$l1
  l2=$l2
  l3=$l3
 """);
  
 var l4 = [...l1, ...l2]; //该方式会将l1 和 l2中的内容平铺出来放入l4
 print("l4=$l4");

运行后输出结果:

 l1=[1, 2, 3]
 l2=[4, 5, 6]
 l3=[[1, 2, 3], [4, 5, 6]]
  
 l4=[1, 2, 3, 4, 5, 6]

如果扩展过来的list有可能为空时,可以使用 null-aware 扩展操作符(…?)来避免产生异常

  var l5 = null;
  var l6 = [1, 2, 3, ...?l5];
  print("l6=$l6");

运行后输出结果:

l6=[1, 2, 3]

4.5.2 使用for和if初始化

下面是一种特殊使用方式,将l7的list中的true和false 分别对应成1和0赋值到新的list变量l8中

  var l7 = [true, false, true, false, true, true];
  var l8 = [
    for (var i in l7)
      if (i) 1 else 0
  ];
  print("l7=$l7\nl8=$l8");

运行后输出结果:

l7=[true, false, true, false, true, true]
l8=[1, 0, 1, 0, 1, 1]

4.5.3 List常用方法

  var l7 = [true, false, true, false, true, true];
  var l8 = [
    for (var i in l7)
      if (i) 1 else 0
  ];
  print("l8.length=${l8.length}");
  print("l8.reversed=${l8.reversed}");
  print("l8.first=${l8.first}");
  print("l8[1]=${l8[1]}");

运行后输出结果:

l8.length=6
l8.reversed=(1, 1, 0, 1, 0, 1)
l8.first=1
l8[1]=0

4.6 Map类型

Map<String, String> m1 = new Map(); //声明一个空的map对象
  m1["e"] = "else";
  print("m1.isEmpty=${m1.isEmpty}, m1=$m1");

  var m2 = {"a": "acrobat", "b": "backup", "c": "class", "d": "description"};
  print('m2.length=${m2.length},m2.containsKey("b")=${m2.containsKey("b")}');
  m2.remove("c");
  print('m2.length=${m2.length},m2=$m2');

//将map中全部的value打印出来
  m2.forEach((key, value) {
    print(value);
  });
  
  print("m2.values=${m2.values}");

运行后输出结果:

m1.isEmpty=false, m1={e: else}
m2.length=4,m2.containsKey("b")=true
m2.length=3,m2={a: acrobat, b: backup, d: description}
acrobat
backup
description
m2.values=(acrobat, backup, description)

这个Map类型在做软件开发时,使用率会很高,特别对于代码效率优化方面的效果很明显。
Dart中的Map类型比较简单,同比较的话相当于
swift 中的 Dictionary
objectiveC中 NSDictionary
java和kotlin中的HashMap
不同语言平台使用方式上,基本是一样的如果想学习HashMap的实现原理可以查看HashMap原理深度详解

4.7 Sets集合

集合特点为 无序、无重复、访问快,使用简单。

 Set<int> s1 = <int>{};
 s1.add(1);
 s1.add(2);
 s1.add(3);
 s1.add(3);
 s1.add(3);
 s1.add(5);
 print("s1.lenth=${s1.length}, s1.contains(4)=${s1.contains(4)}, s1=$s1");

运行后输出结果:

s1.lenth=4, s1.contains(4)=false, s1={1, 2, 3, 5}

可以将一个集合元素添加到另一个集合中

 var s2 = {"ab", "ac", "ad"};
 var s3 = {"ac", "ad", "ae"};
 s2.add("af");
 s2.addAll(s3);
 print(s2);

运行后输出结果:

{ab, ac, ad, af, ae}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xuanwenchao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值