Dart Keywords
In Dart programming, keywords are reserved words that have special meanings and functions within the language. They are part of the syntax and cannot be used for any other purpose, such as naming variables or functions. Keywords help define the structure and behavior of Dart programs. Below is an overview of the various keywords in Dart, categorized by their functionality.
Categories of Keywords in Dart
- Data Types
- Control Flow
- Access Control
- Function and Method
- Exception Handling
- Concurrency
- Miscellaneous
1. Data Types
- int: Represents integer values.
- double: Represents floating-point numbers.
- String: Represents a sequence of characters (text).
- bool: Represents a boolean value (
true
orfalse
). - dynamic: A type that can hold values of any type, determined at runtime.
- var: Used for variable declaration where the type is inferred.
- void: Indicates that a function does not return a value.
2. Control Flow
- if: Used to execute a block of code conditionally.
- else: Specifies an alternate block of code to execute if the
if
condition is false. - else if: Allows checking multiple conditions in sequence.
- switch: Executes one block of code among many based on the value of an expression.
- case: A label within a
switch
statement that specifies a block of code to execute. - default: A fallback case in a
switch
statement if nocase
matches. - for: Starts a loop that iterates over a set number of times or through a collection.
- while: Starts a loop that continues as long as a specified condition is true.
- do: Similar to
while
, but guarantees that the block of code is executed at least once. - break: Exits a loop or a
switch
statement. - continue: Skips the current iteration of a loop and proceeds to the next iteration.
3. Access Control
- class: Defines a new class.
- extends: Indicates that a class is inheriting from another class (superclass).
- implements: Specifies that a class implements a particular interface.
- abstract: Marks a class or method as abstract; cannot be instantiated directly.
- static: Indicates that a member belongs to the class rather than instances of the class.
- final: Specifies that a variable can be assigned only once.
- const: Indicates that a variable is a compile-time constant.
- get: Defines a getter method for a property in a class.
- set: Defines a setter method for a property in a class.
- this: Refers to the current instance of a class.
4. Function and Method
- void: Indicates that a function does not return a value.
- return: Exits a function and optionally returns a value.
5. Exception Handling
- try: Starts a block of code that may throw an exception.
- catch: Handles an exception thrown in a
try
block. - finally: A block that always executes after
try
andcatch
, regardless of whether an exception was thrown. - throw: Used to throw an exception.
- assert: Used for debugging purposes to check a condition during development.
6. Concurrency
- async: Marks a function as asynchronous, allowing the use of
await
within it. - await: Pauses the execution of an asynchronous function until the awaited future completes.
7. Miscellaneous
- library: Declares a library in Dart.
- part: Indicates that a file is part of a larger library.
- import: Imports another Dart library or file.
- export: Exports library members for use in other libraries.
- typedef: Defines a new function type.
- is: Checks if an object is of a particular type.
- as: Casts an object to a specific type.
- with: Used for mixin applications in classes.
- static: Used to declare static methods or fields.
- final: Used for variables that can be set only once.
Summary of Dart Keywords
Keyword | Description |
---|---|
int | Represents integer values. |
double | Represents floating-point numbers. |
String | Represents text. |
bool | Represents a boolean value. |
dynamic | A type that can hold any type of value. |
var | Used for variable declaration with type inference. |
void | Indicates a function does not return a value. |
if | Conditional execution. |
else | Alternate code block for if . |
for | Starts a loop for iteration. |
class | Defines a new class. |
extends | Indicates class inheritance. |
try | Begins a block that may throw an exception. |
catch | Handles exceptions thrown in a try block. |
async | Marks a function as asynchronous. |
await | Pauses execution until a future completes. |
import | Imports another Dart library or file. |
Conclusion
Keywords are essential components of Dart's syntax and functionality. They enable you to define data types, control flow, manage access, handle exceptions, and more. Familiarity with these keywords is crucial for writing effective Dart programs and leveraging the language's features. Understanding how and when to use these keywords will help you write clean, efficient, and maintainable code.