preface
Some days ago, I tried to use Flutter to develop some functions in my company. To write a good Flutter program, the Dart language is the foundation.
Of course, in practical development, there may be time to start Flutter development without knowing all aspects of Dart. You can learn and use Flutter as you go.
In order to form a complete system, I would like to give a systematic introduction to Dart language and then start with an introduction to Flutter.
Dart is much more flexible than Java, and I think even more expressive than Kotlin. Dart can be used not only for client development, but also for Web front-end and back-end development.
So, I think Dart is still a language worth learning, so let’s start our Dart learning journey
This is the first article on Dart, which introduces the definition and type system of variables in the Dart language
Hello World
Traditionally, learning a language begins with Hello World. Here’s a look at Dart’s Hello World application:
main(){
var str = "Hello world!";
print(str);
}
Copy the code
- First we define the top-level main function, the entry to the program. If there is no return value, the return type of the function can be omitted
- We then define a string using the var keyword, and STR is a string variable by type inference
- Finally, the STR variable is output by the print function of the system
Definition of variables
Dart supports type derivation, so variables can be defined using the var keyword
The type of the specified variable can also be displayed, for example:
// Use the var keyword to define variables, omitting the specific type var STR ="Hello world!"; // Explicitly specify the variable type String STR ="Hello World!"
Copy the code
Everything is an object in Dart. Null, functions, and numbers are all objects
So if we define a variable that does not have an initial value, the default value of the variable is null
int lineCount;
assert(lineCount == null);
Copy the code
When defining variables, you can use the final keyword, such as
final String name = "chiclaim";
name = "johnny"; // Error
Copy the code
Variables defined by final are initialized when they are defined and can only be assigned once
We can also use the const keyword when defining variables:
const name = "chiclaim"
name = "johnny"; // Error
Copy the code
The const keyword modifies a variable to indicate that it is a compile-time constant, such as numbers and string literals
The difference between const and final is that final means that the variable can only be assigned once. Const means that not only can the variable be assigned once, but that the variable is a constant
Const not only modifies a variable, but also its value, indicating that the value cannot be modified:
mainVar foo = const []; Unsupported operation: Cannot modify an unmodifiable operation foo[0] = 0; // Unsupported operation: Cannot modify an unmodifiable operation foo[0] = 0; Foo = [1,2,3]; // it can be assigned because foo is not final or const; Foo [0] = 0; // Foo [0] = 0;print(foo);
}
Copy the code
Dart type system
The Dart type system is as follows:
- Numbers (numbers such as integer, floating point, etc.)
- Strings
- Booleans (Boolean value)
- Lists (Collection frames and Arrays)
- Sets (sets)
- Maps
- Runes (express Unicode characters)
- Symbols (generally not used)
numbers
Unlike C/C++/Java, Dart has only two types for representing numbers: int and double
-
Int: indicates an integer with no decimal point. The maximum value is not greater than 64 bits (-2^63 to 2^63-1). The maximum value depends on the underlying system
-
Double represents a floating-point type of double precision
int age = 17; Double weight = 65.5; double waistline = 30; // Equivalent to 30.0Copy the code
Interconversion between string and numeric types:
Parse (int num = int."2"); // Convert string to float double PI = double. Parse ("3.14"); PiStr = 3.14.toString(); piStr = 3.14.tostring (); String piAsStringFix = 3.14159. ToStringAsFixed (2); // String piAsStringFix = 3.14159.Copy the code
String string
String creation
A Dart string is a sequence of UTF-16 code units that can be used to create a string using either single or double quotes
// Double quotes create a string literal var s1 ='Single quotes work well for string literals.'; // Single quotes create a string literal var s2 ="Double quotes work just as well."; // Single quotes create string literals (need to escape) var s3 ='It\'s easy to escape the string delimiter.';
var s4 = "It's even easier to use the other delimiter.";
Copy the code
Create a raw string by prefixing it with r:
// if we want to output \n as a string, we can also use the raw string method'In a raw string, not even \n gets special treatment.';
Copy the code
We can also embed the expression (${expression}) in a string:
const name = "chiclaim"; // Put the expression${expression}Embed var info = in the string"My name is ${name}"; // If the expression is an identifier, you can omit the curly braces var info2 ="My name is $name";
Copy the code
Concatenation of strings:
// 1. Concatenate string var s2 = with + sign'The + operator ' + 'works, as well.'; // 2. The adjacent string literal var s1 ='String '
'concatenation'
" works even over line breaks."; // 3. Pass the multi-line string (in triple quotes) var s1 =' ''
You can create
multi-line strings like this one.
'' ';
var s2 = """This is also a
multi-line string.""";
Copy the code
A common function for strings
1. String query correlation
- Contains (STR) Specifies whether a string is contained in the string
- StartsWith (STR) whether the string startsWith a string
- EndsWith (STR) specifies whether the string endsWith a string
- IndexOf (STR) Position of the string in the target string
// Check whether a string contains another string.
assert('Never odd or even'.contains('odd'));
// Does a string start with another string?
assert('Never odd or even'.startsWith('Never'));
// Does a string end with another string?
assert('Never odd or even'.endsWith('even'));
// Find the location of a string inside a string.
assert('Never odd or even'.indexOf('odd') = = 6);Copy the code
2. String extraction
- Substring (startIndex,endIndex) String interception
- Split (Pattern) String split
- Length Length of the string
- [index] Retrieves the UTF-16 encoding unit in the string by index
- CodeUnits returns an unmodifiable List of UTF-16 encoding units of strings
// Assert ('Never odd or even'.substring(6, 9) == 'odd'); // Use a regular expression to split a string, returning the string collection var parts ='structured web apps'.split(' '); // UtF-16 assert('Never odd or even'[0] = ='N'); // Loop to print the string within the stringfor (var char in 'hello'.split(' ')) {
print(char); } // Get all utF-16 characters in the string var codeUnitList ='Never odd or even'.codeUnits.toList(); // The ASCII coded decimal of N is 78 assert(codeUnitList[0] == 78);Copy the code
3. Case conversion
- ToUpperCase converts all strings toUpperCase
- ToLowerCase converts all strings toLowerCase
// Convert to uppercase assert('structured web apps'.toUpperCase() == 'STRUCTURED WEB APPS'); // Change to lowercase assert('STRUCTURED WEB APPS'.toLowerCase() == 'structured web apps');
Copy the code
4. Removing the leading and trailing Spaces of the string and the empty string
- Trim Removes header and trailing whitespace
- IsEmpty whether the string isEmpty
- IsNotEmpty whether the string isNotEmpty
// remove Spaces at the beginning and endprint(' hello '.trim() == 'hello'); // Whether the string is emptyprint(' '.isEmpty); // Space is not an empty stringprint(' '.isNotEmpty); // The space trim becomes an empty stringprint(' '.trim().isNotEmpty);
Copy the code
5. Partial string replacement
- replaceAll
Strings are immutable, and instead of modifying the original string, the substitution function produces a new string
var greetingTemplate = 'Hello, NAME! ';
var greeting = greetingTemplate.replaceAll(RegExp('NAME'), 'Bob');
// greetingTemplate didn't change. assert(greeting ! = greetingTemplate);Copy the code
6. String construction
To programmatically generate a string, you can do this using StringBuffer, until the stringbuffer.tostring () function is called
var sb = StringBuffer(); / / by.. Implement chain call sb.. write('Use a StringBuffer for ') // Writes a list of strings, separated by Spaces.. writeAll(['efficient'.'string'.'creation'].' ')
..write('. '); Var fullString = sb.tostring (); assert(fullString =='Use a StringBuffer for efficient string creation.');
Copy the code
Boolean
Dart describes booleans using the bool keyword, which has two values: true and false. True and false are compiler constants
bool isSuccess = true;
bool isFailed = false;
Copy the code
List
Lists are used not only to represent lists (ordered collections of data), but also to represent arrays
Now how do I create a List literal
var list = [1, 2, 3]; // List<int>Copy the code
When we introduce the const keyword, we also use const to modify the List as follows:
var constantList = const [1, 2, 3];
// constantList[1] = 1; // Error.
Copy the code
Dart2.3 provides the expansion operator (… And… ?). Make it easier for developers to insert multiple values into collections
var list = [1, 2, 3]; // Expand operator (...) spread operator var list2 = [0, ...list]; var list; // Expand operator (... ?). null-aware spread operator var list2 = [0, ...?list];Copy the code
Dart2.3 provides expansion operators as well as collection if and collection for
// collection if
var nav = [
'Home'.'Furniture'.'Plants'.if (promoActive) 'Outlet'
];
// collection for
var listOfInts = [1, 2, 3];
var listOfStrings = [
'# 0'.for (var i in listOfInts) '#$i'
];
assert(listOfStrings[1] == '# 1');
Copy the code
Collection if/for is useful when developing Flutter and can simplify a lot of code
Set
Set represents an unordered Set. How do I create a Set literal
// halogens is Set<String>
var halogens = {'fluorine'.'chlorine'.'bromine'.'iodine'.'astatine'};
Copy the code
Create an empty Set:
Var names = <String>{}; // Explicitly specify the Set type Set<String> names = {}; Var names = {};Copy the code
Similarly, the expansion operator and collection if/for can be used in sets
Map
A Map is a set of key-value pairs. Here’s how to create a Map literal:
// gifts is Map<String, String>
var gifts = {
// Key: Value
'first': 'partridge'.'second': 'turtledoves'.'fifth': 'golden rings'
};
// nobleGases is Map<int, String>
var nobleGases = {
2: 'helium',
10: 'neon',
18: 'argon'};Copy the code
Note that the Key can appear only once, otherwise it will be replaced by the later one
Use parentheses [] to add, modify, or retrieve elements in a Map:
var gifts = Map(); // add gifts['first'] = 'partridge';
var gifts = {'first': 'partridge'}; Get the assert(gifts[) element in the Map by Key'first'] = ='partridge');
var gifts = {'first': 'partridge'}; // Modify the gifts[element in the Map'fourth'] = 'calling birds';
Copy the code
Similarly, the expansion operator and collection if/for can be used in sets
More on the Dart expansion operator and collection if/for can be found:
Dart expansion operators and Control Flow Collections for Flutter learning
Rune
In Dart, the Rune type is the UTF-32 code point for a String, and we mentioned code units when we introduced String
So before we get to Rune, what are code points and code units?
The Unicode code defines a numeric representation for all letters, numbers, and symbols in the world
- Code point: Unicode is the scope of the coded character set. All it does is map each character in the list of characters that we need to represent to a number, which is called code points for the corresponding character
- Symbol: refers to the number of characters stored in the code point. For example, UTF-8 is 8 bits (1 byte), and UTF-16 is 2 bytes
Because Dart strings are sequences of UTF-16 codes, strings describing 32-bit Unitcode require special syntax
Generally, Unicode uses \uXXXX to describe code points, where XXXX is a four-digit hexadecimal value, such as the symbol \u2665 to describe hearts
If there are more or less than four hexadecimal numbers, curly braces are required. For example, use \u{1f600} for laughing expressions.
The String class has several attributes that extract the String’s rune information (for example, the attribute runes). The codeUnitAt and codeUnit attributes return 16 bit code units.
main() {
var clapping = '\u{1f44f}';
print(clapping);
print(clapping.codeUnits);
print(clapping.runes.toList());
Runes input = new Runes(
'\u2665 \u{1f605} \u{1f60e} \u{1f47b} \u{1f596} \u{1f44d}');
print(new String.fromCharCodes(input));
}
Copy the code
The Object and the dynamic
All classes in Dart inherit from Object, similar to Java. Dynamic and Object allow all values to be assigned to it:
dynamic name;
Object email;
main() {
name = "chiclaim";
email = "[email protected]";
}
Copy the code
But they are two very different things. You can even think of Dynamic not as a type, dynamic just means dynamic, and it just tells the compiler not to do type checking
dynamic name;
Object email;
main() {// assign String to name name ="chiclaim"; // Prints the length of nameprint(name.length); // assign int to name name = 1; // NoSuchMethodError: Class'int' has no instance getter 'length'.
print(name.length);
email = "[email protected]"; // The compiler reported an error because Object has no length attributeprint(email.length);
}
Copy the code
From the above code examples, you have a good idea of the difference between Dynamic and Object
Dart’s best practices on the website advise developers to use Object instead of Dynamic if you want to mean any Object
If you allow type inference to fail, which is usually pushed to failure, the compiler will default to dynamic, but it is recommended to explicitly write dynamic, because the reader of the code won’t know if you forgot to write the type or if:
MergeJson (original, changes){}Copy the code
Dart has several important concepts
- Everything is connected to objects: NULL, functions, numbers
- Dart provides type inference. You can define a variable without specifying a type. If you do not want to specify a type, you can use the dynamic type
- Dart supports generics such as List
, List
- Dart supports top-level functions, member and static functions, and nesting of functions
- Dart does not have the public, private, or protected keywords to control access. Dart controls access with an underscore, which indicates that it is visible only in the Library
Reference
That’s all for Dart variable and type systems. For more information on Android, check out my GitHub: github.com/chiclaim/An…
The dart. Dev/guides/lang… Github.com/acmerfight/…
To contact me
The following is my public number, dry goods articles not bad, there is a need to pay attention to, there are any questions can contact me: