Flutter common data types and data type conversion

Brief introduction

Since the Dart is a language, and then the same language as we know, there are basic data types as well as common flow processing grammar, then we come to understand the next.

Dart everything is an object, including numbers, functions, and so on. They are inherited from Object, default is null (including digital), so numbers, strings can call a variety of methods.

Always variables

variable

Var variable declaration used, may impart different types of values; when not initialized, the default value is null; with the final declaration of a variable can only be assigned once

main void () { 
  
  // declare a variable a 
  var a; 
  Print (a); // value of a print, the display is null 

  a = 10; 
  Print (a); // print out the result of 10 

  a = "Test string "; 
  Print (a); // print out the results of a test string 

  a = 30; 
  Print (a); // print out the result of 30 

  Final B = 10; 
  B = 20 is; // this error will , prompted: [DART] 'B', A Final variable, CAN BE SET Once only. 

}

  

constant

Use const declare a constant in dart in; use const declaration must be compile-time constants.

main void () { 
  const A = 10; 
  A = 20 is; // also given here: [DART] by Constant Variables CAN BE Not Assigned A value. 
}

  

In the above we can see some of the simple syntax dart. In fact, quite like js and the data type of a variable declaration is just to change them, you do not have a fixed data type (for example: java language). You can not be modified after the second is final and can declare const variables, and declared variables.

Simply put, the difference between here and the next final of const:

final required variable is initialized only once, does not require that the value assigned must be compile-time constants, it can be constant or not. The const requirements initialized when the statement, and the assignment is necessary for the compile-time constant.

Dart built-in type

In Dart several built-in data types: numeric -Number, Boolean -boolean, key-value pair -Map, strings -String, list -List, other types -Runes, Symbols

Numeric

Dart only offers two types:

NUM
. 1, shaping int
2, double float

main void () { 
  
  NUM A = 10; // shaping 
  a = 20.1; // float 

  int = I 10; 
  I = 10.1; // this place being given, because an int type data to double type 

  double d 20.1 =; 
  D = 20 is; // this place being given, because of the double type to data type int 
}

  

From the above it can see if a variable is declared using num, free conversion type, but if you are using an int or double clear statement, it can not be converted


Numeric operation

Operators: +, -, *, /, - /,%

Common attributes: isNaN, IsEven, IsOdd

Method used: ABS (), round (), floorl (), ceil (), toInt (), toDouble ()

// Since conventional method has in other languages, not repeat the past, the following are some specific 
void main () { 
  
  int I = 10; 
  Double D = 20.1; 
  
  Print (I / D); // 0.49751243781094523 
  print (i ~ / d); // 0 this operation is rounding 

  print (i.isOdd); // Analyzing odd 
  print (i.isEven); // Analyzing is even 
}

Some common conversion

// String -> int
var one = int.parse('1');
assert(one == 1);

// String -> double
var onePointOne = double.parse('1.1');
assert(onePointOne == 1.1);

// int -> String
String oneAsString = 1.toString();
assert(oneAsString == '1');

// double -> String
String piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == '3.14');

String

I was able to use single or double quotes in the string dart in a statement. Both methods can be.

main void () { 
  String String = 'single quotes declaration string'; 
  String string1 = "double-quoted statement string"; 
}

In the case of each double quotes nested single String

void main () { 
  
  String STR = "single quotation marks" double quotation marks " '; 
  String str1 =" double quotes' single quotes' "; 
  Print (STR); 
  Print (str1); 

  String str2 =" single quotes the \ 'single quote \' '; 
  String Str3 = "double quotes \" double quotation marks \ ""; 
  Print (str2); 
  Print (Str3); 
}

String concatenation way

The following show various operations of the dart in the string concatenation (black magic) listed.

main void () { 
  // spaces using stitching, a plurality of spaces may 
  String str1 = 'single quotes space string' splice '' ~ '; // string of single quotes spaces splicing ~ 

  @ line breaks and space 
  String str2 = 'single quote character string' 
    'for the trip' plus space 'splice'; // swap single quote character line stitching together the spaces 

  // single or double quotes space splicing 
  String str3 = "single or double quotation mark space the string " 'splice'" ~ "; // ~ Mono quoted string space splicing 

  @ single or double quotation marks and spaces newline 
  string str4 =" single or double quotes string ' 
    ' for the trip 'plus space' splice '; // single or double quotation mark character swap line plus space stitching 

  String str5 =' '' 
    to use three single quotes, 
    see I use it 
  '' '; 
          

  String STR6 = "" " 
    using three pairs of quotation marks, 
    see I use it to 
  "" ";

  String str7 = "Well, little or normal" + ", used to splice +"; 
}

From the above it can be seen dart can use the space to do for the stitching. So we continue to study next.

main void () { 

  // string concatenation, output: single or double quotation marks blank spaces 
  String blockStr = 'single quotes' 'space'; 
  String blockStr1 = 'single quotes' 'space'; 
  String blockStr2 = "double quotation marks" " space "; 
  String blockStr3 =" double quotation marks "" space "; 

  // error situations like these will 
  // String blockStr4 = 'single quotes' '' 'space'; 
  // String blockStr5 =" double quotation marks "" "" space "; 

  intermediate test // add characters 
  String blockStr6 = 'single quotes' '_' 'space'; // output: single quotes _ space 
  String blockStr7 =" double quotation marks "" # "" space "; // note can not be used $, $ being given, output: single quote # spaces 

  // single or double quotation marks mix 
  String blockStr8 = 'single "" "" quotes'; // output: single "" ""Quotes 
  String blockStr9 = 'single "" _ "" quotes'; // Output: Single "" _ "" quotes 

  single or double quotes using mixed // 
  String blockStr10 = "double' '' 'quotes"; // Output: bis' '''quotation marks
  String blockStr11 = "double '' _ '' quotes"; // Output: double '' _ '' quotes 
}

Use $ {} of the expression

Similar to the use of the expression in JS ES6 above, the feeling is the same.

main void () { 

  var = In Flag to false; 
  var STR = "test String"; 

  Print ( "we authentication string: $ {str}" "Then let our In Flag: $ {} In Flag"); 
  // last output is: we have to verify the string: a test string and then look at our Flag: false 
}

Boolean value

assert is a language built-in predicate function, valid only in test mode
during development, unless the condition is true, otherwise it will throw an exception. (Assertion fails, the program is terminated immediately).

// Check whether the empty 
var the fullName = ''; 
Assert (fullName.isEmpty); 

// Check to 0 
var Hitpoints = 0; 
Assert (Hitpoints <= 0); 

// check whether null. 
Var Unicorn; 
Assert (Unicorn null ==); 

// check whether NaN3. 
var iMeantToDoThis = 0/0; 
Assert (iMeantToDoThis.isNaN);

List List

List of the Examples as follows

main void () { 

  // create configured using 
  var = new new List List (); 

  // create a type of int List 
  List intList = [. 1, 2,. 3]; 

  // create a constant List, can not be changed List 
  List = const constList [10,. 7, 23 is]; 

}

Array common method dart

main void () { 

  // the array can be stored in a plurality of different types of objects 
  var List = [1, 2,. 3, 'Flutter', to true]; 
  Print (List); // output result of: [1, 2 ,. 3, Flutter, to true] 

  // modify the array index value of 2 
  List [2] = 'Dart'; 
  Print (List); // output results: [. 1, 2, Dart, Flutter, to true] 

  // Get the length of the array of 
  print (list.length); // output results: 5 

  // add elements to the array 
  List.add ( "value"); 
  Print (List); // output result of: [1, 2, Dart, Flutter, to true, value] 

  // add an element to a specified position in the array 
  list.insert (. 1, 'element');   
  Print (List); // output result of: [1, element, 2, Dart, Flutter , to true, value] 

  // remove elements array 
  list.remove ( "value"); 
  Print (List); // output result of: [1, element, 2, Dart, Flutter, true]

  // remove the specified array element 
  list.removeAt (. 1); 
  print (List); // output results: [. 1, 2, Dart, Flutter, to true] 

  // determines whether there is an element array 
  print ( list.indexOf ( "element")); // output: -1 

  // can sort () function to sort, but since we are using different array type definition, can not be used 
  var intlist = [1, 2, 5, 6, 3]; 

  // The syntax Tip: list.sort ([(int, int) → int Compare]) → void 
  intlist.sort ((A, B) => a.compareTo (B)); 
  Print (intlist); // output: [1, 2, 3, 5, 6] 

}

map collection

Create a Map collection

main void () { 

  // Create the Map 
  var = {Language 'the fisrt': 'DART', 'SECOND': 'Java'}; 

  // Create immutable the Map 
  var constLanguage = const { 'the fisrt': 'DART', 'SECOND': 'Java'}; 

  // created by the constructor 
  var new new initLanguage the Map = (); 
}

Map commonly used method

main void () { 

  // Create the Map 
  var = {Map 'the fisrt': 'DART', 'SECOND': 'Java'}; 

  // get the length of 
  print (map.length); // output: 2 

  // Analyzing is empty 
  print (map.isEmpty); // output: false 
  Print (map.isNotEmpty); // output: to true 

  // get to all of Key 
  Print (map.keys); // output: (fisrt , SECOND,) 

  // get all the values 
  Print (map.values); // output: (DART, the Java) 

  // determine whether to include a Key 
  Print (map.containsKey ( "Key")); // output results: to false 

  // comprises determining whether a value 
  Print (map.containsValue ( "Key")); // output: to false 

  // Add a new element into the 
  Map [ 'THIRD'] = 'Key'; 
  Print ( map); // output: {fisrt: dart, second: java,third: key}

  // 循环打印 代码提示:Map.forEach((String, String) → void f) → void
  map.forEach( (key, value) =>  getMap(key, value) );
}


void getMap(key, value){
  print("key:${key}, value:${value}");
}

  

dynamic keyword

This keyword is defined on a similar case we define a variable in JS. Look at the following code

main void () { 
  // A can change the data type, the type is dynamic 
  var A; 
  A = 10; 
  A = "DART"; 

  // here we use the dynamic statement 
  dynamic D = 10; 
  D = 'DART' ; 
}

  

Original Address: https://blog.csdn.net/qq_18948359/article/details/82749659

Guess you like

Origin www.cnblogs.com/gxsyj/p/10973657.html