Dart series: built in types in dart language

Keywords: Flutter dart


brief introduction

Like all programming languages, dart has built-in language types. These built-in types inherit from Object. Of course, these built-in types are the basis of dart language. Only by mastering these built-in types can we be handy when using dart language.

Today, let's talk about the built-in types of dart language.


Null is used to represent null in dart. So what's the relationship between null and null?

Null is a class. First 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";

You can see that the string representation of null type is null. The corresponding null is a keyword, which corresponds to the null class.


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

int represents an integer no greater than 64 bits. Because dart can run on different platforms, the scope represented by different platforms is also different.

On native platforms, such as android or IOS, int can range from - 2 ^ 63 to 2 ^ 63 - 1. However, in the web environment, the representable range is - 2 ^ 53 to 2 ^ 53 - 1

double represents a floating-point type.

For numbers, basic operators like +, -, / and * are defined in the num class. Of course, there are other regular operators.

If you need more complex operations, you can use the dart:math library.

Here are some examples of the use of numbers:

int age =18;
int number= 20;
double money = 10.1;

character string

String is a type that is often used. The class corresponding to the string in dart is string. It can also be expressed directly in literal terms as follows:

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

Strings can be expressed in single quotation marks or double quotation marks. The string in dart uses UTF-16 encoding.

The string in dart can also carry variable values in the format ${expression}

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

The two strings can be compared with = = to see whether they are equal. The characters compare the corresponding character coding sequences. If the character coding sequences are equal, the corresponding strings are equal.

Strings can be connected using +.

var hello ="hello " + "word";

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!

By default, the character representation in a string is the character itself. If you want to convert it to its original meaning, you can add r before the string:

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

Boolean value

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

Because dart is type safe, that is, when you need to use bool type, you can't use other types instead.

For example, if we want to judge whether the string is empty, we can judge it as follows:

var name='';
    do something


The List in dart is represented by List. Of course, it can also be represented directly by the following literal:

var list = [1, 2, 3];

The index of the list starts from 0 and ends with length-1.

Starting from dart 2.3, the extender... And nullable extender...?, are introduced, The extender can be used to expand a known list into its corresponding elements, so as to easily build the combination of lists:

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

dart provides a magical function, that is, you can use if and for statements to dynamically generate the elements in the list during the construction of the list:

var nav = [
  if (promoActive) 'Outlet'


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

set and map

Set s in dart are represented by sets.

set represents a collection of non repeating elements, as shown below:

var names = {'jack', 'mark', 'max'};

The mapping in dart is represented by Map.

The creation of Map is similar to that of set, but contains key and value:

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

You can find that set and map are very similar, so the problem is, how to represent an empty set or map?

Because the elements in the set are single and the elements in the map are key value pairs, we can express them as follows:

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

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

var map = {};

To get the value in the map, you can use:

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

Both map and set support extenders... And nullable extenders, if and for operations within collections are also supported.

This article has been included in http://www.flydean.com/02-dart-buildin-type/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to find!

Welcome to my official account: "those things in procedure", understand technology, know you better!

Posted by KindMan on Mon, 08 Nov 2021 22:24:46 -0800