“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

[toc]

Introduction to the

Dart, like all programming languages, has built-in language types that are derived from Object. These built-in types are the foundation of dart.

Today I’ll explain the built-in types of the DART language.

Null

Dart uses null to indicate null. So what does null have to do with null?

Null is a class. Let’s look at the definition of Null:

class Null {
  factory Null._uninstantiable() {
    throw UnsupportedError('class Null cannot be instantiated');
  }

  external int get hashCode;

  /** Returns the string `"null"`. */
  String toString() => "null";
}
Copy the code

You can see that the string representation of type Null is Null. The corresponding NULL is a keyword and corresponds to the NULL class.

digital

The number in DART corresponds to num, which has two subclasses int and double.

Int Indicates an integer not larger than 64 bits. Because DART can run on different platforms, the range represented on different platforms is different.

On native platforms, such as Android or IOS, int can range from -2^63 to 2^ 63-1. But in a Web environment, the representable range is -2^53 to 2^ 53-1.

The corresponding double represents a floating point type.

For numbers, basic operators such as +, -, /, and * are defined in the num class. There are, of course, other general operators.

If you need more complex calculations, you can use the DART: Math library.

Here are a few examples of numbers:

int age =18;
int number= 20;
double money = 10.1;
Copy the code

string

A string is a type that is often used. The corresponding class for a String in DART is String. It can also be expressed literally as follows:

var name ='jack';
var site ="www.flydean.com";
Copy the code

Strings can be expressed in single or double quotes. Dart uses UTF-16 encoding for strings.

Dart also includes variable values in a string of the form ${expression}.

var age=10; var words ='your age is ${age}! ';Copy the code

Two strings can be compared for equality by using ==. Characters compare the corresponding character encoding sequence. If the character encoding sequence is equal, the corresponding strings are equal.

Concatenation of strings can use +.

var hello ="hello " + "word";
Copy the code

Another way to create a string is to use three single quotes or three double quotes.

var string1= '''
this is a string!
''';

var string2 = """
this is string again!
""";
Copy the code

By default, the character representation in a string is the character itself. To convert to its original meaning, we can prefix the string with r:

var string3 =r'this is line one \n this is line two';
Copy the code

Boolean value

Booleans are represented in dart as bool. The bool value is represented by only two strings, true and false.

Dart is type-safe, which means that when a bool type is needed, it cannot be replaced by another type.

For example, if we want to determine whether a string is empty, we can do this:

var name='';
if(name.isEmpty){
    do something
}
Copy the code

The list of

Dart lists are represented by lists, or you can use the following literals:

var list = [1, 2, 3];
Copy the code

The index of the list starts at 0 and ends with Length-1.

Beginning with DART 2.3, extenders have been introduced… And nullable extenders… ? , the extension can be used to expand a known list into its corresponding elements, which can be used to easily construct a list combination:

var list = [1, 2, 3];
var list2 = [0, ...list];
Copy the code

Dart provides an amazing feature that allows you to dynamically generate elements of a list using the if and for statements:

var nav = [
  'Home',
  'Furniture',
  'Plants',
  if (promoActive) 'Outlet'
];
Copy the code

Or:

var listOfInts = [1, 2, 3];
var listOfStrings = [
  '#0',
  for (var i in listOfInts) '#$i'
];
Copy the code

The set and map

Sets in DART are represented by sets.

Set represents a collection of non-repeating elements, as follows:

var names = {'jack', 'mark', 'max'};
Copy the code

Mappings in DART are represented by maps.

A Map is created in much the same way as a set, but with keys and values:

var students = {'jack':18, 'mark':19, 'max':20};
Copy the code

Set and map are very similar, so how do you represent an empty set or map?

Since the elements in a set are single and the elements in a map are key-value pairs, we can represent them as follows:

var names = <String>{};
var gifts = Map<String, String>();
Copy the code

However, if the type is not specified, a map is created by default:

var map = {};
Copy the code

To get the values in the map, use:

var gifts = Map<String, String>();
gifts['first'] = 'partridge';
gifts['second'] = 'turtledoves';
Copy the code

Both map and set support extenders… And nullable extenders… ? If and for operations within collections are also supported.

This article is available at www.flydean.com

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!