[Flutter development practice] Dart basics: the most basic syntax content

Insert image description here

Before we understand the Dart programming language in depth, we need to understand some of the most basic knowledge about Dart, such as constants, variables, functions, etc., so that our development efficiency can be improved to the next level. In this section, we'll explore some basic syntax, including entry methods main, variables, constants, and naming conventions. Let us uncover the mystery of Dart together and bring you a deeper understanding.

Exploring the door of Dart: main method

In Dart, all program execution begins with mainmethods. This is the entry point of the program, where code execution begins. Many beginners will start reading from the main method when learning code to sort out the overall context of the entire project. Let's look at a simple main method:

void main() {
    
    
  print("Hello, Dart!");
}

This is a simple case that many friends will write when they first learn programming. In this example, mainthe method is the starting point of the program, and the function is used to output text on the console. When you press Run, you will see this paragraph printin the console. Hello, Dart!talk. Dart's syntax is concise, allowing you to understand and write code quickly.

Variables: store memory of the world

In Dart, variables are used to store data. You can declare a variable using varthe keyword and Dart will automatically infer its type. For example:

void main() {
    
    
  var message = "Hello, Dart!";
  print(message);
}

Here, we created a messagevariable named to store a string; when we click Run, messagethe contents stored in will be output. Dart's type inference eliminates the need to explicitly specify variable types, making the code more concise.

Constants: an unchanging world

Unlike variables, constants are immutable in Dart. Constants are usually declared using the finalor keyword. Indicates a compile-time constant whose value must be assigned at the beginning . It does not need to assign an initial value when it is declared, but it can only be assigned once at runtime . The difference is that it is a runtime constant, and it has the characteristics of lazy initialization, that is, it is not initialized until it is used for the first time at runtime.constconstfinal constfinal

void main() {
    
    
  final int hoursInDay = 24;
  const double gravity = 9.8;

  print("A day has $hoursInDay hours, and gravity is $gravity m/s².");
}

Here, we create a finalconstant that represents the number of hours in a day and consta constant that represents the gravity of the Earth. The use of constants helps improve code readability and performance.

Naming rules: Things to note when naming variables

In Dart, you need to pay attention to some common issues when naming variables and constants, otherwise it will cause some unnecessary errors. Here are some of the "can's" and "can'ts" when naming variables:

  • Variable names should consist of numbers, letters, underscores and dollar signs ( $), such as myVariable, user123, totalAmount, _internalVar, $price.
  • The identifier cannot start with a number, 123variablewhich 7daysOfWeekis a bad example.
  • Identifiers cannot be reserved words and keywords. class, if, import, voidthese exist in keywords and reserved words and cannot be used as variable names.
  • Variable names are case-sensitive. ageand Ageare two different variables, please pay attention to the distinction.

The following are some reserved words and keywords in Dart:

Reserved words:

abstract as covariant deferred dynamic
export external factory Function get
implements import interface late mixin
on set static sync typedef

Keywords:

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

Good naming rules are the key to writing clear and understandable code. Here are some commonly used naming conventions that we recommend you use:

  • Variable and function names should use camelCase, such as myVariableor calculateTotal.
  • Class names should use Pascal notation, for example MyClass.
  • Avoid using abbreviations unless they are well known.
void main() {
    
    
  String favoriteColor = "Blue";
  int numberOfApples = 10;

  print("My favorite color is $favoriteColor, and I have $numberOfApples apples.");
}

In the above code, we use camel case naming to give variables clear and easy-to-understand names, making the code more readable.


Before exploring the world of Dart, first understand mainthat methods are the door; variables are the storage of memory, varallowing you to relax; constants are the unchanging universe, finaland constare as smart as different; naming rules are like poetry for naming variables, and the names have meanings. Don't get too carried away. On the stage of Dart, simplicity, fun and clarity are the protagonists!

Author information

Author: Fanyi
CSDN: https://techfanyi.blog.csdn.net
Nuggets: https://juejin.cn/user/4154386571867191

Guess you like

Origin blog.csdn.net/qq_21484461/article/details/135446758