Built-in types

  • numeric
  • string
  • The Boolean
  • List/array
  • Map
  • Runes (Unicode characters in strings)
  • symbols

Variables of the above types can be initialized with literals, e.g. ‘this is a string’ is a string literal, and true is a Boolean literal

Dart variables are referenced objects, so they can be initialized using constructors. Some built-in variables have their own constructors, such as Map().

numeric

There are two numerical types of dart:

  • Int: an integer whose bit width depends on the platform but is not larger than 64 bits
  • Double: a 64-bit double precision floating point number defined by IEEE754

Int and double are subtypes of num, including the addition, subtraction, multiplication, and division (+ – * /) operations, abs(), ceil(), floor(), and so on. (Displacement operation >> defined in int). If the basic operations do not meet the requirements, the DART: Math library can be used.

After dart2.1, integer literals can be automatically converted to double: double z = 1; // Equivalent to double z = 1.0

String and integer conversions

// 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');
Copy the code

The bit operation, and operation, or operation of an integer

assert((3 << 1) = =6); // 0011 << 1 == 0110
assert((3 >> 1) = =1); // 0011 >> 1 == 0001
assert((3 | 4) = =7); / / 0011 | 0100 = = 0111
Copy the code

Numeric literals are compile-time constants. Arithmetic expressions consisting of compile-time constants are also compile-time constants.

const msPerSecond = 1000;
const secondsUntilRetry = 5;
const msUntilRetry = secondsUntilRetry * msPerSecond;
Copy the code

String

Dart uses the UTF-16 character set and can be created with either single or double quotation marks.

var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
var s3 = 'It\'s easy to escape the string delimiter.';
var s4 = "It's even easier to use the other delimiter.";
Copy the code

Use ${expression} to use the value of an expression in a string. If the expression is an identifier, you can omit the curly braces.

    var s = 'string interpolation';

    assert('Dart has $s, which is very handy.'= ='Dart has string interpolation, ' + 'which is very handy.');
    assert('That deserves all caps. ' + '${s.toUpperCase()}is very handy! '= ='That deserves all caps. ' + 'STRING INTERPOLATION is very handy! ');
Copy the code

In DART, == is used to determine whether a string is equal as long as the character content is the same.

Concatenation strings can use the + operator or adjacent string literals.

    var s1 = 'String '
        'concatenation'
        " works even over line breaks.";
    assert(s1 ==
        'String concatenation works even over '
        'line breaks.');

    var s2 = 'The + operator ' + 'works, as well.';
    assert(s2 == 'The + operator works, as well.');
Copy the code

Create a multi-line string: use three single or double quotation marks.

    var s1 = '''
You can create
multi-line strings like this one.
''';

    var s2 = """This is also a
multi-line string.""";
Copy the code

Raw string: Escape characters in it are not processed

var s = r'In a raw string, not even \n gets special treatment.';
Copy the code

For more information see Strings and Regular Expressions

The Boolean

Dart uses bool to represent Boolean data. Only two objects have bool: true and false (Boolean literals, compile-time constants)

Dart’s type safety means that the following code is not available

if(nonbooleanValue)...assert(nonbooleanValue)
Copy the code

The list of

The List object in DART represents an array, or List.

Var list = [1, 2, 3]; , will be automatically inferred as List. If non-int objects are inserted after it, error will occur. Please refer to Type inference.

The list index starts at 0 and ends at lsit.length-1.

Creating compile-time constants lists requires the const keyword in front of the list of literals. var constList = const [1, 2, 3];

See Generics and Collections for more information

Maps

Key-value structure. Dart is represented by Map.

    var gifts = {
      // Key: Value
      'first': 'partridge'.'second': 'turtledoves'.'fifth': 'golden rings'
    };

    var nobleGases = {
      2: 'helium'.10: 'neon'.18: 'argon'};Copy the code

Gifts = Map<String, String>; NobleGases indicates Map<int, String>.

Map objects can be created using the Map constructor, where the new keyword can be added or omitted.

    var gifts = Map(a); gifts['first'] = 'partridge';
    gifts['second'] = 'turtledoves';
    gifts['fifth'] = 'golden rings';

    var nobleGases = Map(a); nobleGases[2] = 'helium';
    nobleGases[10] = 'neon';
    nobleGases[18] = 'argon';
Copy the code

See Generics and Maps for more information

Runes

Runes is the encoding unit in DART that represents utF-32 format characters. In Dart, runes are the UTF-32 code points of a string.

Unicode defines a unique value for all letters, numbers, and symbols in use around the world, but DART’s character sequence uses the UTF-16 encoding, so representing utF-32 Unicode values requires special syntax.

A common Unicode encoding is \uXXXX, which is a 4-bit hexadecimal value, such as \u2665, enclosed in curly braces when representing more or less than 4 bits, such as \u{1f600}.

The String class provides attributes for retrieving rune information, such as 16-bit encoding of bytes for codeUnitAt and codeUnit; Get the runes of the string using the runes attribute.

The relationship between RUNES, 16-bit coding units, and 32-bit coding units can be shown in the following code.

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

How do I Reverse a String in Dart? How do I Reverse a String in Dart?

Symbols

Please refer to the description on the official website