The second day of Flutter study: A summary of commonly used data types in Dart, full of dry goods, too friendly for those who have learned Python and java?

This is the second day of my learning Flutter, because learning Flutter must learn Dart language, Dart is the main development language of Flutter. My first feeling is that Dart’s grammar seems to be a combination of python and java. Although he also has some grammars of his own, on the whole, he mainly learned a language, most of which are interlinked, but it is possible The expression is different.

Common data types of Dart

Little knowledge about printing to the console
单个数据的话 ::

num a=1;
print(a);

多个数据的话:
$符号后面跟你需要打印的数据, And our% functions of java and Python are quite similar.

num a=1,b=3;
print("a:$a b:$b");

如果数据是下面这种形式的话: If the
output is the data of two methods, it needs to be enclosed in {}.

List list=[1,2,3];
print("数据1:${list.indexOf(1)} 数据2:${list.indexof(2)}"}

1.num

num:, 数值型变量can store (integer, floating point).

 num num1=-1.0;
 num num2=2;
 num num3=3.18;
 print("num:$num1 num:$num2 num:$num3");

Output:
Insert picture description here

2.int, double type

int int1=3;
double d1=1.68;
print("int:$int1 double:$d1");

Output:
Insert picture description here
num method:

 num num1=-1.0;
 print(num1.abs()); //求绝对值
 print(num1.toInt()); //转换成int
 print(num1.toDouble()); //转换成Double

3.String type

1. can 双引号或者单引号be assigned.
2. There are 两种字符串拼接methods, as shown in the code below.

 String str1='字符串',str2="双引号";  //可以用双引号,或者单引号
 String str3='str1:$str1 str2:$str2';  //字符串拼接
 String str4='str1:'+str1+' str2:'+str2;   //字符串拼接
 print(str3)
 print(str4)

Output:
Insert picture description here
Common methods of string:

String str5='常用数据类型,请空控制台输出输出';
print(str5.split('类型'));   //字符串在类型处进行分割存入集合
print(str5.substring(1,5));   //字符串截取
print(str5.indexOf('类型'));  //获取指定字符串位置
String str6 = 'https://www.baidu.com/';
print(str6.startsWith("https"));   //检测字符串是否以指定的子字符串开始
print(str6.endsWith("/"));    //检测字符串是否以/结尾
print(str6.toUpperCase());    //所有小写字母转化为大写
print(str6.toLowerCase());    //所有大写字母转化为小写
print(str6.length);          //长度
print(str6.replaceFirst('t', 'p'));  //只替换字符串里面的第一个出现的t
print(str6.replaceAll('t','p'));  //替换数据,把字符串里面所有的t替换成p

Output:
Insert picture description here

4.bool type

bool success=true,fail=false;
print(success);
print(fail);
print(success||fail); //true   或
print(success&&fail); //false   与

Output:
Insert picture description here

5.List type

1.List type添加数据的两种方式

List list=[1,2,3,4,'集合'];   //初始化时添加元素
print(list);
List list3=[];
list3.add('list3');    //通过add方法添加元素
list3.addAll(list);    //把list中数据添加到list3中

Output:
Insert picture description here

2. Other methods:

Assignment between sets:, 把list中的值传递给list2about dynamic, I will explain below.

List list=[1,2,3,4,'集合'];   //初始化时添加元素
print(list);
List<dynamic> list2=[];
list2=list;
print(list2);

The generate method is similar to the Pythonrange method.

 List list4=List.generate(3, (index) => index*2);
 print(list4);

Output:
Output

3. Three methods of List type traversal:

List list=[1,2,3,4,'集合'];
for(int i=0;i<list.length;i++){
    
    
  print(list[i]);
}

for(var o in list){
    
    
  print(o);
}

list.forEach((val) {
    
    
  print(val);
});

4. Common methods

List list=[1,2,3,4,'集合'];   //初始化时添加元素
print(list.remove('集合'));  // 删除第一个匹配的数据
print(list.removeAt(3));  //删除第三个数据
list.removeRange(2, 3);   //删除下标为2的数据
print(list);
print(list.indexOf(3));   //找到第一个满足条件的下标
list.insert(2, 'json'); //插入数据
print(list);
list.insertAll(2, ['bad','good']);   //插入一个数组
print(list);
List list1=[1,2,3,4,6];
list1.retainWhere((value) => value % 2 == 0);
print(list);   //删除不满足的条件

Insert picture description here

6.Map type

1.Map type添加数据的两种方式

Map names={
    
    'xiaoming':'小明','xiaohong':'小红'};
print(names);
Map ages={
    
    };
ages['xiaoming']=16;
ages['xiaohong']=18;
print(ages);

Output:
Insert picture description here

2. Map type traversal

ages.forEach((key, value) {
    
    
 print('$key,$value');
});
for (var key in ages.keys) {
    
    
  print('$key ${ages[key]}');
}

Output:

3. Pass values ​​between Map types

Map ages2=ages.map((key, value){
    
    
	return MapEntry(key, value);
});
print(ages2);

Insert picture description here

7. The difference between dynamic, var, and Object

My learning video teacher said this:

  dynamic:是所有Dart对象的基础类型, 在大多数情况下,通常不直接使用它,
  通过它定义的变量会关闭类型检查,这意味着 dynamic x = 'hal';x.foo();
  这段代码静态类型检查不会报错,但是运行时会crash,因为x并没有foo()方法,所以建议大家在编程时不要直接使用dynamic;
  var:是一个关键字,意思是“我不关心这里的类型是什么。”,系统会自动推断类型runtimeType;
  Object:是Dart对象的基类,当你定义:Object o=xxx;时这时候系统会认为o是个对象,你可以调用o的toString()和hashCode()方法
  因为Object提供了这些方法,但是如果你尝试调用o.foo()时,静态类型检查会进行报错;
  综上不难看出dynamic与Object的最大的区别是在静态类型检查上;

1. Personally think that dynamic, object and var are still very easy to distinguish, as shown below:

(Which runtimeType method is to view what kind of)
Dynamic and Object: 开始x和o1都是字符串类型,把他们赋值为int类型不会报错. becauseDynamic is the basic type of all Dart objects, Object is the base class (parent class) of Dart objects.
Insert picture description here
Insert picture description here
var: if 开始为字符串类型,赋值为int类型会爆红. becauseThe first assignment of var will determine its type
Insert picture description here

2. The difference between dynamic and object,dynamic与Object的最大的区别是在静态类型检查上

dynamic: can be seen x可以进行++操作. becauseThe reason why dynamic does not require type conversion is that dynamic skips compilation type checking.
Insert picture description here
Object: proceed o1++操作的话会爆红.The object is not type converted and cannot be directly calculated
Insert picture description here

Continuous learning. . . . .

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/113004892