Dart Reference
Notes on the Dart language
Updated: 03 September 2023
Notes from the The Net Ninja Youtube Series
To get started with Dart you can use DartPad
Hello World
A simple Hello World in dart looks like so:
In Dart we require semicolons and the entrypoint is the main
function
Variables
We declare variables like this:
Some data types are:
int
String
which can use a'
or a"
bool
dynamic
We can also define variables as dynamic
which means that their type can be changed
String interpolation can also be done like so:
Functions
Functions make use of the following structure:
If a function has a single line we can also make use of an arrow function which looks like this:
A function with inputs is defined and used like so:
And used as you’d expect:
Functions can also take in optional params, there are two methods for this, named and unnamed params:
The way we call these functions differ
We can also use list methods to work with the list
The list as defined above does not have a defined type, usually we want to specify the allowable types (coz obviously). We can set the type using the <T>
notation like so:
We also have methods available for lists such as the map
function which will return an IEnumerable
:
And additionally the toList
method which will allow us to convert the above back into a list
Classes
Classes are defined using the class
keyword, with the properties defined within the class. The new
keyword is not needed
Private properties start with
_
and cannot be accessed from outside the class
We can also initialize class variables like this:
We can use inheritance using the extends
keyword:
When using the above we need to be sure to also call the constructor of the base class from the inheriting class using super
Const and Final
A const
variable is one that’s value is constant at compile time, a final
variable is one that is only assigned to once
In dart we can also define constants using the const
and final
keywords like so:
You can also create constant instances of objects but that requres a const
constructor
If we have a class with a constructor but we want to use it as a const constructor, we can do so by using the final
keyword for our property, and the below notation for the constructor:
We can then create an instance of this like so:
Maps
Maps are like dictionaries/key-value pairs
To create a map we use the Map
data type and the []
accessor:
This is used in flutter when doing routing for pages
Async
Async code is code that finishes some time after being called but is not blocking. We use a combination of async
, await
, and Futures
Futures
A function that makes use of a Future that simply does a delay looks like this:
The callback is run when the future completes. This is similar to Promise
in JavaScript
We can then call the above function from our initState
function. A complete example would be something like this:
Async/Await
Sometimes however we have some asynchronous code that needs to run sequentially we can make use of async
and await
Similar to other languages, Futures
(promises) can be awaited within an async
function. If we wanted to make our getData
function run more sequentially we can do something like this (note we also added async
to the function definition):
Exceptions
To handle exceptions in dart we can use a try-catch
:
We can also use an optional finally
Cascade operator
Dart has a cascade operator ..
which allows a function to return the instance of the initial object and not the result of a function call:
Or from the documentation:
“Cascades (..) allow you to make a sequence of operations on the same object. In addition to function calls, you can also access fields on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.”
Conditionals
If-Else
With multiple conditions:
Ternary
We can use a ternary operator like so:
The ternary operator doesn’t have to return a boolean, it can pretty much be anything