]

Dart Programming - Syntax

Published May 07, 2020

Dart language introduction

  • Dart is an open source programming language released by Google;
  • Dart's initial goal is to become the next generation web development language;
  • Dart has now become a full platform development language;
  • Dart is an object-oriented programming language

The grammar defines a set of rules for writing programs, and each language specification defines its own grammar. The composition of Dart grammar is as follows:

  • Type of data
  • Operator
  • Control flow statement
  • method
  • class
  • Comment
  • abnormal
  • Guide package

Code example:

main(){
    print('Hello Dart');
}

 

The main() function is a predefined method in Dart. This method acts as the entry point to the application. A Dart script needs the main() method for execution. print() is a predefined function that prints the specified string or value to the standard output i.e. the terminal

Output

Hello Dart

 

Execute a Dart Program

You can execute a Dart program in two ways −

  • Via the terminal
  • Via the WebStorm IDE

Via the Terminal

To execute a Dart program via the terminal −

  • Navigate to the path of the current project
  • Type the following command in the Terminal window
dart filename.dart

Via the WebStorm IDE

To execute a Dart program via the WebStorm IDE −

  • Right-click the Dart script file on the IDE. (The file should contain the main() function to enable execution)

  • Click on the ‘Run <file_name>’ option

 

Dart Command-Line Options

Sr.No Command-Line Option & Description
1 -c or --c

Enables both assertions and type checks (checked mode).

2 --version

Displays VM version information.

3 --packages <path>

Specifies the path to the package resolution configuration file.

4 -p <path>

Specifies where to find imported libraries. This option cannot be used with --packages.

5 -h or --help

Displays help.

 

Identifier definition rules:
Identifiers define the names of elements in the program, such as variables, functions, etc. The rule is that identifiers can contain characters and numbers, but identifiers cannot begin with numbers

  • Except for underscore (_) or dollar sign ($), the identifier cannot contain special symbols
  • The identifier cannot be a keyword
  • Must be unique
  • The identifier is case sensitive
  • Cannot contain spaces

Dart keywords:

Built-in keywords:

  • abstract
  • as
  • deferred
  • operator
  • typedef
  • dynamic
  • get
  • implements
  • set
  • export
  • import
  • static
  • external
  • factory
  • library

New keywords in Dart2.0:

  • async
  • async*
  • await
  • yield
  • sync*
  • yield*

Reserved words:

  • continue
  • false
  • new
  • this
  • default
  • final
  • null
  • throw
  • assert
  • finally
  • true
  • do
  • for
  • try
  • rethrow
  • else
  • if
  • return
  • where
  • break
  • enum
  • void
  • case
  • while
  • catch
  • in
  • super
  • with
  • class
  • extends
  • is
  • switch
  • const

Blank and newline:

Dart ignores spaces, tabs, and newlines that appear in the program.

 

Dart is case-sensitive.

This means that Dart differentiates between uppercase and lowercase characters.

The statement ends with a semicolon.Every
statement ends with a semicolon (;).

 

Dart comments:
comments can improve the readability of the code and make it easier to understand.Comments can contain the meaning of programs, methods, properties, and variables, and the compiler will ignore the comments;

Dart supports comment types:

Single line comment: //, / *** /, ////
Multi-line comment: ///

 

Object-Oriented Programming in Dart

Dart is an Object-Oriented language. Object Orientation is a software development paradigm that follows real-world modelling. Object Orientation considers a program as a collection of objects that communicate with each other via mechanism called methods.

  • Object − An object is a real-time representation of any entity. As per Grady Brooch, every object must have three features −

    • State − described by the attributes of an object.

    • Behavior − describes how the object will act.

    • Identity − a unique value that distinguishes an object from a set of similar such objects.

  • Class − A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object.

  • Method − Methods facilitate communication between objects

Example

class MyClass {   
   void disp() {     
      print("Hello Dart"); 
   } 
}  
void main() {   
   MyClass c = new MyClass();   
   c.disp();  
}

Output

Hello Dart

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

261 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions