×

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

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Dart If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 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 Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 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.

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.