Getting Started With Dart: The Basics
Diving deep into the fundamentals of dart lang.
Alright, so, chances are that if you’re learning Dart, you probably want to make apps using Flutter. While that was never the goal of why Dart was developed(originally meant to replace JavaScript), it’s one of the most promising languages for the future.
Developed at Google, Dart is an object-oriented programming language which can be compiled to machine code, transpiled to JavaScript or can be run as an interpreted language.
Although Google has thorough documentation, I personally had trouble going over all the topics and would’ve really benefited from an article like this; This article is essentially a crash course to Dart. Assuming this is not your first language, let’s go over the basics of this programming language and the default data structures
To follow along, even if you don’t have the Dart SDK installed, use this link.
The main() function
Every app must have a top-level main()
function, which serves as the entry point to the app. The main()
function returns void
and has an optional List<String>
parameter for arguments.
Here’s an example of the main()
function :
Variables
Dart supports both, dynamic and static typing. What that means is that the data type of the variable does not have to be explicitly declared at the time of initialization. However, in case you want to specify a type, that is possible. The var keyword initializes the myString variable who’s type is inferred as a String. It also allows the possibility of changing the data in the variable. If you want a variable whose value can not be altered later on in your program, use final
or const
.
Dart has several built-in types including String, int, double, boolean.. etc
Conditional Statements
Like all programming languages, Dart has conditional statements.
Loops
Loops are extremely handy for repeating events occurring in our program which you should not really type out.
Lists, Maps, Sets
Lists
Lists use zero-based indexing, where 0 is the index of the first element and list.length — 1 is the index of the last element. While initializing list specifying the data type is optional.
Sets
A set in Dart is an unordered collection of unique items. Dart support for sets is provided by set literals and the Set type.
Maps
In general, a map is an object that associates keys and values. Both keys and values can be any type of object. Each key occurs only once, but you can use the same value multiple times. Dart support for maps is provided by map literals and the Map type.
Functions
Dart is a true object-oriented language, so even functions are objects and have a type, Function. This means that functions can be assigned to variables or passed as arguments to other functions. Type of passed arguments need not be specific (although it’s best practice to do so)
// The basic syntax for defining a function is<return type>functionName(parameters){(function body)}
In line functions
Optional Parameters
Often times, while calling functions, the user isn’t always aware as to what they should use as arguments. Optional parameters come in handy. They are usually enclosed in square braces []
Classes & Object-Oriented Programming
Dart is an object-oriented language with classes and mixin-based inheritance. Every object is an instance of a class, and all classes descend from Object. Mixin-based inheritance means that although every class (except for Object) has exactly one superclass, a class body can be reused in multiple class hierarchies. Extension methods are a way to add functionality to a class without changing the class or creating a subclass.
Extending a class
Use extends
to create a subclass, and super
to refer to the superclass:
Overriding members
Subclasses can override instance methods, getters, and setters. You can use the @override
annotation to indicate that you are intentionally overriding a member:
Enumerated types
Enumerated types, often called enumerations or enums, are a special kind of class used to represent a fixed number of constant values.
Using enums
Declare an enumerated type using the enum
keyword:
That’s pretty much it! If you understood all the topics, you’re off to a great start. Next step: Flutter. Good luck and happy coding!