Assuming that you already know how to program in other languages, learn more about the Dart core library by referring to the Dart library Overview. Dart supports single-line, multi-line, and document comments
✅ Tip: Open DartPad to experience most of Dart’s language features (learn more)
Important concepts
** Version tip: Dart 2.12 introduces null security. Air security requires a language version of at least 2.12.
- All variables refer toobjectEach object is oneclassThe instance. Numbers, functions, and
null
They’re all objects. removenull
If you turn it onAir safety), all classes inherit from the [Object][] class. - Although Dart is a strongly typed language, specifying a type when declaring a variable is optional because Dart can do type inference. In the code above, variables
number
Type is inferred to beint
Type. - If you turn it onAir safety, variables cannot be if they are not declared as nullable types
null
. You can do this by placing a question mark (?
) declares the type as nullable. For example,int?
Variables of type can be integer numbers ornull
. If you areClearly knowAn expression is never empty, but when Dart doesn’t think so, you can add it after the expression!
To assert that the expression is not null (an exception is thrown if it is null). Such as:int x = nullableButNotNullInt!
- If you want to explicitly declare that arbitrary types are allowed, use
Object?
(if youAir security is enabled),Object
orSpecial typedynamic
Defer checking until run time. - Dart supports generics, such as
List<int>
(represents a list of int objects) orList<Object>
Represents a list of objects of any type. - Dart supports top-level functions (e.g
main
Method), but also supports defining functions that belong to classes or objects (i.estatic 和 Instance methods). You can also define functions within functions (nested 或 Local function). - Dart supports top-level variables, as well as variables (static and instance variables) that are defined as belonging to classes or objects. Instance variables are sometimes called fields or properties.
- Dart has nothing like Java
public
,protected
和private
Member access qualifier. If an identifier is underlined (_
) indicates that the identifier is private in the library. You can refer toLibraries and visibilityFor more information. - identifierCan start with a letter or underscore (
_
), followed by a combination of characters and numbers. - In the Dartexpression 和 statementsExpressions have values and statements do not. Such asConditional expression
expression condition ? expr1 : expr2
Contains the valueexpr1
或expr2
. withIf-else branch statementCompared to theif-else
A branch statement has no value. A statement usually contains one or more expressions, but an expression cannot contain only one statement. - The Dart tool displays both warning and error types of problems. Warnings indicate that there may be a problem with the code but do not prevent it from running. Errors are classified as compile-time errors and runtime errors; Compile-time error code does not run; Runtime errors cause exceptions while the code is running.
The keyword
The following table lists the keywords used by the Dart language.
1 column | 2 columns | 3 columns | Four columns |
---|---|---|---|
abstract 2 | else | import 2 | show 1 |
as 2 | enum | in | static 2 |
assert | export 2 | interface 2 | super |
async 1 | extends | is | switch |
await 3 | extension 2 | late 2 | sync 1 |
break | external 2 | library 2 | this |
case | factory 2 | mixin 2 | throw |
catch | false | new | true |
class | final | null | try |
const | finally | on 1 | typedef 2 |
continue | for | operator 2 | var |
covariant 2 | Function 2 | part 2 | void |
default | get 2 | required 2 | while |
deferred 2 | hide 1 | rethrow | with |
do | if | return | yield 3 |
dynamic 2 | implements 2 | set 2 |
You should avoid using these words as identifiers. However, words with superscript can be used as identifiers if necessary:
- Keywords with superscript 1 are context keywords that make sense only in specific scenarios, and they can be used as valid identifiers anywhere.
- Keywords with superscript 2 are built-in identifiers, just to make it easier to convert JavaScript code into Dart code. These keywords are valid identifiers most of the time, but they cannot be used as class or type names or as import prefixes.
- With a superscript3Is used after Dart 1.0 is releasedSupport asynchronousRelated content. Cannot be in by keyword
async
,async*
或sync*
Used in the method body of the identityawait
或yield
As an identifier.
Other keywords without superscript are reserved and cannot be used as identifiers.
annotation
Dart supports single-line, multi-line, and document comments.
Single-line comments
Single-line comments start with //. Anything between // and the end of the line is ignored by the compiler.
void main() {
// TODO: refactor into an AbstractLlamaGreetingFactory?
print('Welcome to my Llama farm! ');
}
Copy the code
Multiline comment
Multi-line comments start with /* and end with */. Anything between /* and */ is ignored by the compiler (document comments are not ignored), and multi-line comments can be nested.
void main() {
/* * This is a lot of work. Consider raising chickens. Llama larry = Llama(); larry.feed(); larry.exercise(); larry.clean(); * /
}
Copy the code
Documentation comments
A document comment can be a multi-line or single-line comment, starting with /// or /**. Using // on consecutive lines has the same effect as multi-line document comments.
In a document comment, the parser ignores all text unless it is enclosed in brackets. Use brackets to refer to classes, methods, fields, top-level variables, functions, and parameters. The symbols in parentheses are resolved in the lexical field of the documented program element.
Here is a document comment that references other classes and members:
/// A domesticated South American camelid (Lama glama).
///
/// Andean cultures have used llamas as meat and pack
/// animals since pre-Hispanic times.
///
/// Just like any other animal, llamas need to eat,
/// so don't forget to [feed] them some [Food].
class Llama {
String? name;
/// Feeds your llama [food].
///
/// The typical llama eats one bale of hay per week.
void feed(Food food) {
// ...
}
/// Exercises your llama with an [activity] for
/// [timeLimit] minutes.
void exercise(Activity activity, int timeLimit) {
// ...}}Copy the code
In the generated document, [Feed] becomes a link to the document for the feed method, and [Food] becomes a link to the API document for the Food class.
Parse the Dart code and generate HTML documents using Dart’s document generation tool. For an example of generating documentation, refer to Dart API Documentation for recommendations on document structure: Guidelines for Dart Doc Comments.