Dart Data Types

The Green Coder
4 min readJan 8, 2021

--

I started my coding journey by learning ruby on rails, I then progressed to Javascript, during the last leg of my journey at FlatIron school, I chose to make an application in Flutter, which uses the Dart Language. What is Dart and How is it Different from Javascript? For starters, it is an Object Oriented Programming language which means it adheres to the four pillars of Object-Oriented Programming. Dart for the most part is also considered a strongly typed language. It does have some dynamic typing so it can be considered optionally typed as well. But generally in Dart, I found the best practice is to steer clear of dynamic variables. Like TypeScript, Dart requires that you declare the data type of any variable that you create when creating the variable. What does that mean exactly? Say you want to create a new variable for a name that a user will enter. In Javascript, it would look something like this.

const name = “ ”

In Javascript, and other languages that are weakly typed, this is acceptable because these languages do not declare the variable type at compile-time, only at runtime, so if something other than a string gets stored as the name variable in the example above, there would be no errors at compile-time, however, if a different data type was added, it would still break at runtime.

A strongly Typed language has you write a variable like this:

const String name = “ ”

We declare that this variable is a string data type, and so the program knows that if anything other than a string is stored in that variable, it won't allow it. The system checks at compile time and will error out. This helps to cut down on errors immensely and keeps the app running more smoothly.

Coming from Javascript, this took some getting used to, but once I did, I really appreciated the extra checks and balance it provides.

Data types in Dart are pretty standard, but understanding how to write them correctly can take practice. Here is the list of Dart’s data types and how to write them when declaring a variable.

Strings

Strings variables act the same in Dart as other languages, they store characters — letters, numbers, and special characters — in a variable declared with the String keyword. They are represented with either single or double quotes in Dart.

Integers

Integers come in a couple of different flavors with Dart. Depending on if the data will be a whole number or a decimal, you would use “int” for a whole number and the “double” keyword for a decimal. Or it can be declared using “num” to accept both whole numbers or decimals.

Booleans

Booleans variables are used to store true or false values. The value is stored as either true or false. The keyword bool is placed before a boolean variable.

Lists

A list is used to store a collection of ordered objects. This is similar to an array in Javascript, where a set of values is stored in a single variable. Each value stored is separated by a comma and can be called upon using the index number. Also like an array, a list is defined using the square bracket syntax. The keyword list is used to declare a list variable.

Maps

Maps are objects that contain a set of key-value pairs. Each key must be unique, and the value associated with a key can be called by using the key. This is synonymous with an Object in Javascript, or a Hash in Ruby. The Map is defined using curly braces {}, and a key and value pair is associated with a colon. Like the other data types, you declare a map with the Map keyword before the variable name.

Dynamic

The last data type I will cover is Dynamic. Dynamic is meant to be unspecified meaning the variable can be any data typed and is declared at runtime usually. This is the same way Javascript handles data types and it is why Dart can be considered an optionally typed language. It's best to avoid using dynamic variables unless the datatype will truly be unspecified at compile time. They are defined using the dynamic keyword.

--

--

The Green Coder
The Green Coder

Written by The Green Coder

0 Followers

Hello! I’m Jason and I'm starting the journey of becoming a great software developer! I want to share my experiences with all the other green coders out there!

No responses yet