1 introduction of the Dart
Dart is an open source, general-purpose programming language. It was originally developed by Google as a replacement for the front-end JS, but as JS exploded, Dart’s position became awkward and tepid. That is, until Flutter comes to Dart’s rescue again. Dart has a lot in common with JS, but it also has some unique features. Let’s take a look at Dart in this series of articles.
2 Variable Declaration
var
❝
Dart, like JavaScript, uses var to declare variables, but the type of a variable is determined once it is assigned to Dart and cannot be changed otherwise it will return an error.
❞
var a = 1;
a = "123"; // The compiler will warn
Copy the code
Final and const
❝
The use of final and const in Dart is no different from that in other languages. Final means that a variable can only be assigned once and cannot be changed after the assignment or an error will be reported. Const represents a compile-time constant. Variable types that are final or const can be omitted.
❞
final a = 1; //final int a = 1
const b = "abc"; //final String b = "abc"
Copy the code
The dynamic and Object
❝
Object is the base class of all objects, and all types are subclasses of Object, so all variables can be declared as Object. Variables declared by Object and Dynamic can also be assigned to any Object. Object and Dynamic are similar to var in this respect, except that the type of variables declared by Object and Dynamic can be changed after assignment.
❞
dynamic a = 1;
a = "123";
print(a); // No error, output 123
Object b = 2;
b = "abc";
print(b); // no error, output ABC
Copy the code
❝
The difference between Object and Dynamic is that Object can only use Object methods. The Object compiler for dynamic declarations provides all possibilities.
❞
dynamic a = 1;
a = "123";
print(a.length); 3 / / output
Object b = 2;
b = "abc";
print(b.length); Error: The getter'length' isn't defined for the class 'Object'.
Copy the code
3 Basic data type
❝
Dart uses five common data types: Number, String, Boolean, List, and Map.
❞
Number
❝
Number contains two data types, int for integer and double for floating point. An integer can be automatically converted to a floating-point type when declared, but a floating-point type cannot be automatically converted to an integer.
❞
double a = 1; // No problem
Int b = 1.1; / / complains
Copy the code
- Common properties and methods
attribute | describe |
---|---|
isOdd | Check whether it is odd |
isEvent | Check whether it’s even |
isNaN | Check if it’s a number |
abs() | Take the absolute value |
ceil() | Returns an integer not greater than its maximum |
floor() | Returns an integer not less than its smallest |
toString() | Convert to string |
round() | The nearest integer |
remainder() | The remainder of that |
int a = 1;
int b = -1;
Double c = 1.4;
print(a.isEven); \ \false
print(a.isNaN); \ \false
print(a.isOdd); \ \true
print(a.toString()); \ \ 1
print(c.floor()); \ \ 2
print(c.round()); \ \ 1
print(c.ceil()); \ \ 1
print(b.abs()); \ \ 1
print(a.remainder(2)); \ \ 1
Copy the code
String
❝
String values in Dart can be expressed in single, double, or triple quotes. Single-line strings are represented by single or double quotation marks. Triple quotes are used to represent multi-line strings. Dart can concatenate strings with + and also supports interpolation of ${}.
❞
String a = "hello";
String b = "world";
print(a + ""+ b); \\ Output hello world
int c = 1;
print("This is ${c}"); \\ Output This is 1
Copy the code
- Commonly used methods and properties
attribute | describe |
---|---|
isEmpty | Check whether it is empty |
length | The length of the |
subString() | Interception of substring |
toLowerCase() | All lowercase letters |
toUpperCase() | All uppercase characters |
split() | Divide a string into lists |
indexOf() | Returns the position of a character |
startWith() | Judge the beginning of a string |
replaceAll() | Substitution string |
contains() | Determines whether a substring is included |
String a = "Hello";
String b = "a b c d";
print(a.isEmpty); \ \false
print(a.length); 5 \ \
print(a.substring(2)); \\llo
print(a.toLowerCase()); \\hello
print(a.toUpperCase()); \\HELLO
print(b.split("")); [a, b, c, d]
print(a.indexOf("o")); 4 \ \
print(a.startsWith("H")); \ \true
print(a.replaceAll("l"."o")); \\heooo
print(a.contains("ll")); \ \true
Copy the code
Boolean
❝
Dart Boolean has only two values true and false.
❞
List
❝
A List in Dart is similar to an array in other languages, being an ordered collection of objects. You can declare either a List of fixed length or a List of indefinite length.
❞
List list1 = new List(3); // Declare a List of fixed length
List list2 = new List(); // Declare an indefinite List
/ / the List initialized
list1[0] = 0;
list1[1] = 1;
list1[2] = 2;
print(list1); Output [0, 1, 2]
Copy the code
- List Common attributes and methods
attribute | describe |
---|---|
length | The length of the List |
first | List first element |
last | The last element of the List |
reversed | Turn to the List |
isEmpty | Check whether List is empty |
asMap | Convert a List to a Map |
add | Add elements to List |
clear | To empty the List |
remove | Remove elements |
List list1 = new List(3); // Declare a List of fixed length
List list2 = new List(); // Declare an indefinite List
/ / the List initialized
list1[0] = 0;
list1[1] = 1;
list1[2] = 2;
print(list1.length); / / 3
print(list1.first); / / 0
print(list1.last); / / 2
print(list1.reversed); / / (2, 1, 0]
print(list1.isEmpty); //false
Map map = list1.asMap();
print(map); //{0:0, 1:1, 2:2}
list2.add(1);
print(list2); / / [1]
list2.clear();
print(list2); / / []
Copy the code
Map
❝
A Map object is a simple key/value pair. Keys and values in a Map can be of any type. A Map is a dynamic collection. In other words, a Map can grow and shrink a Map object at run time as a simple key/value pair. A Map can be declared either by a literal or by a constructor.
❞
Map map = {"name" : "Joe"."sex" : "male"}; // declare by literal
Map map1 = new Map(); // declare by constructor
Copy the code
- Common attributes and methods of Map
attribute | describe |
---|---|
length | The length of the Map |
keys | Returns a list of keys |
values | Returns a list of values |
containsKey | Check whether the key is included |
containsValues | Check whether value is included |
isEmpty | Check whether it is empty |
remove | Remove an element |
Map map = {"name" : "Joe"."sex" : "male"}; // declare by literal
Map map1 = new Map(); // declare by constructor
print(map.length); \ \ 2
print(map.keys); \ \ ["name"."sex"]
print(map.values); \ \ ["Joe"."male"]
print(map.containsKey("name")); \ \true
print(map.containsValues("John")); \ \false
print(map.isEmpty); \ \false
print(map.remove("name")); \ \"Joe"
Copy the code
The last
Today, we briefly introduced Dart variable declarations and simple data types, but you can write them yourself. Paper come zhongjue shallow, realize this to practice. How we feel good, “like” is the biggest support for more articles we can pay attention to wechat public number QStack, pursue the purest technology, enjoy the joy of programming.