Dart programming syntax

The syntax for defining a set of rules for writing programs. Each language specification defines its own syntax. Dart syntax has the following composition:

  • Variables and operators
  • class
  • function
  • Expression and code block
  • Analyzing and loop structure
  • Note
  • Libraries and packages
  • Type Definition
  • Data structures: set / Generic

Your first dart program

Let the traditional Hello Worldstart example

main() {
   print("你好Dart!");
}

The main()functions are dartpredefined method. This method serves as the entry point to your application. Dart script needs main()a method to execute. print()It is a predefined function, a string, or it specifies the value of the output to the standard output, i.e., the terminal.

Output of the code is:

你好Dart!

Program execution dart

You can execute the program in two ways Dart

  • Through terminal
  • By IDE

Through terminal

Dart program executed by the terminal

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

By IDE

Dart program to be executed by IDE

  • atom : Ctrl + Shift + B
  • vscode: Ctrl + Alt + N
  • WEBSTRA: Ctrl + Shift + F10

Dart command-line options

Dart Dart command-line options for modifying the script execution. Dart common command-line options include the following

No. Command line options and descriptions
1 -c or --c

Enable assertions and type checking (selected models).

2 --version

VM version information is displayed.

3 --packages <path>

Specify a package parsing the configuration file path.

4 -p <path>

Find specify where the imported library. This option can not be used with --packages.

5 -h or--help

Displays help.

Enable check mode

dart program runs in two modes, namely

  • Check mode
  • Production mode (default)

Recommended that during the development and testing inspection mode to run VM Dart
, because it will add warnings and errors to aid in the development and debugging process. The selected mode will enforce various checks, such as the type of inspection. To open the selected mode, add before the script file name when you run a script -cor --checkedoptions.

However, to ensure the performance benefits when running a script, it is recommended in production mode to run the script.

Consider the following Test.dart script file

void main() {
   int n = "hello";
   print(n);
}

Enter the following command to run the script

dart Test.dart

Although there is a type mismatch, but the script will be executed successfully when you close inspection mode. The script produces the following output

hello

Now try to use --checkedor -coption to execute the script

dart -c Test.dart

Either

dart - - checked Test.dart

Dart VM will throw an error stating that there is a type mismatch.

Unhandled exception:
type 'String' is not a subtype of type 'int' of 'n' where
   String is from dart:core
   int is from dart:core
#0  main (file:///C:/Users/Administrator/Desktop/test.dart:3:9)
#1  _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart :261)
#2  _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)

The Dart identifier

Identifier is the name given to the program elements, such as variables, functions, and so on. Rule identifier is

Identifiers can include characters and numbers. However, the identifier can not start with a number.

  • In addition to the underscore ( _) or dollar sign ( $), the identifier can not contain special characters.

  • Identifiers can not be keywords.

  • They must be unique.

  • Identifiers are case-sensitive.

  • Identifiers can not contain spaces.

The following table lists valid and invalid identifiers few examples

Valid identifier Invalid identifier
firstName Where
first_name first name
num1 first-name
$result 1number

The Dart keywords

Key words have special meaning in the context of the language. The following table lists some of the Dart keywords.

abstract 1 continue false new this
as 1 default final null throw
assert deferred 1 finally operator 1 true
async 2 do for part 1 try
async* 2 dynamic 1 get 1 rethrow typedef 1
await 2 else if return where
break enum implements 1 set 1 void
case export 1 import 1 static 1 while
catch external 1 in super with
class extends is switch yield 2
const factory 1 library 1 sync* 2 yield* 2

Blank and line feed

Dart ignores spaces that appear in the program, tabs, and line breaks. You are free to use in the program spaces, tabs and line breaks, and may freely in a concise manner consistent formatting and indenting program, make the code easier to read and understand.

Dart is case sensitive

Dart case sensitive. This means Dart distinguish between uppercase and lowercase characters.

Statement ends with a semicolon

Each command are called statements. Each dart statement must (semicolon ;) at the end. Single line can contain multiple statements. However, these statements must be separated by a semicolon.

Dart comment

Notes is a method to improve readability. Notes can be used to contain additional information about the program, such as the code of the relevant function / configuration of the tips and the like. The compiler will ignore the comment.

Dart supports the following types of comments

  • Single-line comments (//) any text between and the end of the line is treated as a comment

  • Multi-line comments (/ * * /) These comments may span multiple lines.

Case

// 这是一个单行注释  

/* 这是
   多行注释  
*/

Dart object-oriented programming (OOP)

Dart is an object-oriented language. Object-oriented software development paradigm One follows the real-world modeling. Object
Orientation as a collection of program objects communicate with each other via a mechanism called a method.

  • Objects - objects in real-time representation of any entity. The Grady Brooch, each object must have three functions:

  • Status - described by the attributes of the object.

  • Behavior - behavior that describe the object.

  • Identification - the value of the object with a unique set of similar regions of such objects apart.

  • Class - class OOP aspects of the object is to create a blueprint. Class encapsulates the data object.

  • Method - Method to facilitate communication between objects.

Example: Dart and OOP

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

The above example defines a class TestClass. This class has a method disp(). The method print on the terminal "Hello World". newKeyword to create an object class. Which calls the method disp().

Code output

Hello World

This switched: http://codingdict.com/article/21911

Guess you like

Origin www.cnblogs.com/bczd/p/11981952.html