当前位置:网站首页>Dart: Foundation

Dart: Foundation

2022-06-09 04:06:00 @I don't know you

Entry method

Mode one

main() {
    
  print(' Hello dart');
}

Mode two

// Express main Method does not return a value 
void main() {
    
  print(' Hello dart');
}

Variable

dart Is a powerful scripting language , Variable types may not be predefined , Automatic type derivation

Variable definitions

dart Variables defined in can be defined through var Keywords can also declare variables by type .

var str='this is var';
String str='this is var';
int str=123;

notes :var The defined variables are automatically typed ,dart Variables in are strongly typed

Variable naming rule

Same as most languages , Variable naming requires the following conditions :

  • The variable name must be a number 、 Letter 、 Underline and dollar ($) form .
  • Be careful : Identifier cannot start with a number
  • Identifiers cannot be reserved words and keywords .
  • Variable names are case sensitive, such as : age and Age It's a different variable . In practical use , Also suggest , Don't use one word case to distinguish between two variables .
  • identifier ( Variable name ) Be sure to see mingsiyi : It is suggested to use nouns for variable names , The method name suggests the verb

Constant

Dart Medium Constant usage final and const To define .

  • const The value remains the same You have to assign values from the beginning
  • final You can start with no assignment Only once ; and final It's not just const The properties of compile time constants , The most important thing is the runtime constant , also final It's lazy initialization , That is, it is initialized before the first use at runtime

data type

dart The following types are supported :

  • int: plastic
  • double: Double precision floating point
  • String: character string
  • bool: Boolean
  • List: list , Array
  • Maps: Dictionaries , Key value pair

character string

Strings can be used Single quotation marks Double quotes and Three quotation marks define

main() {
    
  var a = ' Single quotation marks ""';
  var b = " Double quotes ''";
  var c = '''
     Three quotes ,
     Three quotation marks can contain single quotation marks '', And double quotes "", No more three quotation marks 
    ''';
  print(a);
  print(b);
  print(c);
}

print Only one variable can be output , It's kind of like console.log Mixed up

String splicing

 Insert picture description here

value type

int The value of type can be converted to double type ,double Type cannot be converted to int type

  double a = 1;    correct 
  int b = 1.2;  error 

list Array

The way 1: Do not specify type

main() {
    
  var a = ['a', 1, false];
  print(a); //[a, 1, false]
  print(a[0]); // a
  print(a.length); //3
}

The way 2: Specify the type

main() {
    
  var a = <int>[1, 2, 3];
  var b = <String>['a'];
  print(a);
  print(b);
}

The way 3:, adopt [] The volume of a collection created can vary

main() {
    
  // Specify a specific type of 
  var a = <int>[];
  print(a); // []
  print(a.length); //0
  a.add(1);
  print(a); //[1]

  // A specific type of... Is not specified 
  var b = [];
  print(b); //[]
  print(b.length); //0
  b.add(1);
  b.add('a');
  print(b); //[1,a]

  // There are values , Only values of the same type can be added 
  // For example, it was all plastic surgery at first , Will default to an integer array , When you add, you can only add shapes 
  var c = [1, 2, 'a'];
  c.add('c');
  print(c); // [1,2,a,c]
}

Maps Dictionaries

The way 1

main() {
    
  var a = {
    'name': ' Zhang San ', 'age': 15}; //key You must also use quotation marks , Cannot item js Without quotation marks 
  print(a); //{name:  Zhang San , age: 15}
  print(a['name']); // Zhang San 
}

The way 2

main() {
    
  var p = new Map();
  p["name"] = " Li Si ";
  p["age"] = 22;
  p["work"] = [" The programmer ", " delivery "];
  print(p); // {name:  Li Si , age: 22, work: [ The programmer ,  delivery ]}
  print(p["age"]); // 22
}

Type judgment

dart Use in is Keyword judgment type

main() {
    
  String a = '1';
  print(a is int); // false
  int b = 1;
  print(b is double); // false
  double c = 1.0;
  print(c is int); // false
  var d = 1;
  print(d is int); // true
}

Operator

Same as the mainstream language , such as Java、JavaScript.

Type conversion

dart It's a strong type of language , Type conversion only supports Number Type conversion to String The type and String Type into Number type

Number Type conversion to String type

 var myNum=12;
 var str=myNum.toString();
 print(str is String);

String Type into Number type

 String str='123';
 var myNum=int.parse(str);
 print(myNum is int);

 String str='123.1';
 var myNum=double.parse(str);
 print(myNum is double);

loop

and c Language makes no difference , Include for loop 、while loop 、do while loop

for loop

for (int i = 1; i<=100; i++) {
      
   print(i);
}

while loop

main() {
    
  int a = 1;
  while (a < 5) {
    
    print(a);
    a++;
  }
}

do while

main() {
    
  int a = 1;
  do {
    
    print(a);
    a++;
  } while (a < 5);
}

break and continue

A little

List Common properties and methods

attribute

length length

 var a = [11, 'a'];
 print(a.length); // 2

reversed Flip

 var a = [11, 'a'];
 print(a.reversed);  // (a,11)

isEmpty Is it empty

 var a = [11, 'a'];
 print(a.isEmpty); // false

isNotEmpty Whether it is not empty

  var a = [11, 'a'];
  print(a.isNotEmpty); // true

first and last The first and last

  var a = [1, 2, 3];
  print(a.first); //1
  print(a.last); //3

Method

add increase

  var a = [];
  a.add(1);
  print(a); // [1]

addAll Mosaic array

  var a = [1];
  var b = [2];
  var c = [3];
  a.addAll(b);
  print(a); // [1,2]

notes :
1、 Cannot splice itself
2、 Only one array can be spliced

indexOf lookup , Returns the first subscript that matches

  var a = [1, 'a', '1'];
  print(a.indexOf('1')); //  Returns the subscript  2
  print(a.indexOf(2)); //  No return found  -1

Parameters 1 Is the value to find ; Parameters 2 Optional , It's the starting position .

indexWhere(): lookup
And indexOf similar , But more advanced , Returns the subscript of the qualified element . Be similar to JavaScript Medium findIndex

 var a = ['ab', 'bc', 'bd'];
 print(a.indexWhere((element) => element.startsWith('b'))); // 1

remove Delete Pass in the specific value

 var a = [1, 'a', '1'];
  a.remove('a');
  print(a); // [1,1]
  print(a[1] is int); // false

removeAt Delete Incoming index value

fillRange modify

  var a = [1, 'a', '1'];
  a.fillRange(0, 2, 'new');
  print(a); // [new, new, 1]

Parameters 1 It's the starting subscript , Parameters 2 Is the termination subscript . Parameters 3 Is the value to change to . Include start not include end .

Be careful : When the starting subscript does not exist , The run will report an error .

insert(index,value); Insert... At specified location

 var a = [1, 'a'];
  a.insert(0, 'new');
  print(a); //[new, 1, a]

insertAll(index,list) Insert... At specified location List

  var a = [1];
  a.insertAll(1, [1, 2, 3]);
  print(a); //[1, 1, 2, 3]

The above can also be achieved by + To achieve

 var a = [1];
 print(a + [2, 3]); // [1,2,3]
 print(a + [1,[2]]);  // error ,var It has the function of default type inference 

fill fill

  var a = new List.filled(2, [], growable: true);
  print(a); // [[], []]
  a[0].add(1);
  print(a); //[[1], [1]]
  a.add([3]);
  print(a); //[[1], [1], [3]]

  var b = new List.filled(2, [], growable: false);
  print(b);
  b[0].add(1);
  print(b);
  b.add([3]);
  print(b); // Report errors 

  var c = new List.filled(2, [], growable: true);
  print(c); // [[], []]
  c[0] = [2];
  print(c); //[[2], []]

notes :
1、growable Whether it can be extended , by true When this list Can continue to expand ; by false Extension... Is not allowed
2、file The padding of is a shared padding , If you use add When adding , Other items will also be added
3、 If you do not want to share the fill, you can directly perform the assignment operation

join() List Convert to string

Same as JavaScript Medium join()

clear Empty List

Traverse

for

A little

for in

for(var item in myList){
    
   print(item);
 }

forEach

A little

map

A little

every

A little

any

A little

where

  List myList = [1, 3, 4, 5, 7, 8, 9];

  var newList = myList.where((value) {
    
    return value > 5;
  });

  print(newList); // (7,8,9)
  print(newList is List); //false
  print(newList.toList()); //[7,8,9]
  print(newList.toList() is List); // true

toList: Turn into List

Set

Dart Inside Set Its main function is to remove the duplicate contents of the array .
Set Is a collection that has no order and cannot be repeated , So you can't index to get the value . You can go through it first toList Turn into list, Then take values according to the index .

  var a = new Set();
  a.add(' Banana ');
  a.add(' Apple ');
  a.add(' Apple ');
  print(a); //{ Banana ,  Apple }
  print(a.toList()); //[ Banana ,  Apple ]

  List myList = [' Banana ', ' Apple ', ' watermelon ', ' Banana ', ' Apple ', ' Banana ', ' Apple '];
  var s = new Set();
  s.addAll(myList);
  print(s); //{ Banana ,  Apple ,  watermelon }
  s.remove(' Banana ');
  print(s.toList()); //[ Apple ,  watermelon ]

Set Support for those that do not need an index List Method

Maps

Definition

 // The first way 
 Map person = {
    "name": " Zhang San ", "age": 20};
 print(person);

 // The second way 
 var m = new Map();
 m["name"] = " Li Si ";
 print(m);

 // The third kind of 
 var a = {
    'name': ' Zhang San '};
 print(a);

  // A fourth , Specifying generics 
  var a = <String, int>{
    'age': 2};
  print(a);

Common properties

  • keys Get all key value
  • values Get all value value
  • isEmpty Is it empty
  • isNotEmpty Whether it is not empty

Common methods

remove(key) Delete the specified key The data of

addAll({…}) Merge mapping Add attributes to the map

var person = {
    "name": " Zhang San ", "age": 20, "sex": " male "};

person.addAll({
    
  "work": [' Knock on the code ', ' delivery '],
  "height": 160
});

print(person);

containsKey and containsValue

 // A fourth , Specifying generics 
 var a = <String, int>{
    'age': 2};
 print(a.containsKey('a')); // Include key or not a
 print(a.containsValue(2)); // Include value or not 2

Traverse

forEach、map、where、any、every. Be similar to List Usage of

原网站

版权声明
本文为[@I don't know you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090354153770.html