×

Dart Miscellaneous types

Some Other Data types in Dart

Apart from the data types studied so far, we have three other data types also which are as follows:

  1. Never
  2. Dynamic
  3. Void

Let us understand each one in detail,

Never data type in Dart

Dart 2.9 offers a new bottom data type known as ‘Never’. Its declaration is there in the core libraries and can be used as a type annotation. It can act as a return type of functions. It has no values and using Never means that the expression can never successfully finish evaluating. It will either throw an exception or abort the execution of the code.

Remember that the static type of a throw expression is Never.

Example : Never fun( ) { }

Dynamic data type in Dart

This data type enables us to create variables that are dynamic in nature. By dynamic means their data type can be changed at the runtime.

For example,

                 dynamic msg = ‘ Hello World ! ’ ;

If instead of this, we have an initialization like this :

                 string msg = ‘ Hello World ! ’ ;

The compiler interprets that the variable ‘ msg ’ is of string type and will only and always hold string values. Even if we ever try to change its data type or to make it hold the integer value for example, we can’t do that.

Program

void main( )
{
      // initializing the string name
      String name = " This is dynamic data type tutorial ! " ;
      
      // printing the string name
      print( name ) ;
  
      // initializing the string type name with integer
      name = 8 ;   
}

Output

Some Other Data types in Dart

As you can see, the compiler threw a compilation error when we tried to assign the string variable ‘name’ with the integer value.

Consider another example where we use dynamic data type and tries to change the value of the variable:

Program

void main( )
{
      // initializing the string name
      dynamic name = " This is dynamic data type tutorial ! " ;
      
      // printing the string name
      print( name ) ;
  
      // initializing the string type name with integer
      name = 55 ;
  
      // printing the integer value of the dynamic variable name
      print( name ) ;
}

Output

This is dynamic data type tutorial !
55

void data type in Dart

void data type in programming languages usually means that they do not return anything. The return type in the Dart language only specifies what the DartAnlyzer warns us about.

If the return type of the function is void, it can return the following three

things :

1 ) Return null

2 ) Return dynamic variable

3 ) Return void function

Remember that we cannot use these returned values.

Constructors

  1. void ( )

Properties

1. hashCode

Returns the hash code for a numerical value, and it returns the same value for int and doubles when the value provided is the same. 

Implementation = external int get hashchode ;

2. runtimeType

Represents the type of runtime an object has. 

Implementation = Type get runtimeType ;

Methods

1. noSuchmethod ( )

This method is called when the non-existing method is accessed.

Implementation =

            dynamic noSuchMethod (

Invocation invocation

)

2. toString ( )

Converts a given number into an equivalent shortest string and returns the same. 

Implementation = external String toString( ) ;


Related Topics

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart If statement

If statement is used to set the control on the lines of code. Using if statement, block of code is executed only if the expression in the statement returns true....

1 minute read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.