In this post, I will be covering the data types available in Dart, how dynamic languages work compared to static languages and which one Dart is, how explicit vs inferred typing works in Dart, and also some important keywords. Let's get started!
Data Types
- Number
- Int
- An integer is a number without decimals.
- https://api.dart.dev/stable/2.13.4/dart-core/int-class.html
- Double
- A double is a number with decimals.
- https://api.dart.dev/stable/2.13.4/dart-core/double-class.html
- int and double are sub-types of num (Numbers). Always prefer using int and double over number.
- Int
- String
- As seen in an earlier post, string is associated with a text output.
- https://api.dart.dev/stable/2.13.4/dart-core/String-class.html
- More in-depth coverage of strings will be in a future post.
- Boolean
- A boolean is either a true or false value.
- https://api.dart.dev/stable/2.13.4/dart-core/bool-class.html
- List
- A list is a collection of ordered data.
- A list can be thought of as similar to an array in JS.
- https://api.dart.dev/stable/2.13.4/dart-core/List-class.html
- Set
- An unordered collection of unique items
- https://api.dart.dev/stable/2.13.4/dart-core/Set-class.html
- Map
- A map is a set of key-value pairs.
- https://api.dart.dev/stable/2.13.4/dart-core/Map-class.html
An important thing to know, but a bit too early to discuss in detail right now, is that everything in Dart is an object. An object is an instance of a class. If you're familiar with classes already and curious on what this means, have a look at the docs for each type that I've linked.
// int
int num1 = 1;
// double
double num2 = 1.0;
// string
String someString = 'hello';
// boolean
bool pass = true;
// list
List<int> myList = [1,3,6];
// set
var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};
// map
var obj1 = {
'firstKey': 1,
'secondKey': 2,
'thirdKey': 3
}
Dynamic vs Static Language
To keep it really short, a dynamic language does not type check until runtime whereas a static language does its type checks at compile-time. What this means is that in a dynamic language, you are free to manipulate your data however you wish without regard to it's type whatsoever. I did my best to make it sound bad, because it is :) This can lead to bugs that you will not notice until it's too late. In a static language, your program won't even compile if there are issues and the IDE will notify you of issues before it's too late. Dart is a static language that can make use of dynamic language feature as well. I will show example of this in a later section.
Var
The var keyword is a way to declare a variable that will change at a later point but with benefit of type inference. Type inference means the compiler will know the type of the variable based on what it's initialized to.
// compiler will know num1 is an int.
var num1 = 1;
// example of explicit typing
// this is unnecessary because the compiler can infer the type for you. Prefer using var.
int num1 = 1;
Const
A const is a way to declare a read-only variable at compile-time. Const can infer a variable's type as well. Use const when a value is known ahead of time.
Final
A final is a way to declare a read-only variable at run-time. Final can infer a variable's type as well. Use final if you will get the value later. For example: an http request.
Dynamic
This is the keyword that would replicate the behavior of a dynamic language. It is allowing the type of the variable to be anything. You should almost never use this.
dynamic value = 'hi';
value = 5;
print(value);
Here, the value is being assigned to a string, but then gets changed to an int after. It may not look problematic in this simple snippet, but it can lead to frustrating bugs in a complex and larger codebase. Let's take a look at another example.
// 1
dynamic val1 = 1;
dynamic val2 = 'hi';
print(val1+val2);
// 2
const val1 = 1;
const val2 = 'hi';
print(val1+val2);
- With dynamic, there wouldn't be any problems until the code is actually executed.
- With const, and making use of type inference, the compiler would already yell at you that there is an issue here before you even try to run this.
Best Practices
- When initializing variables and the variable can change in the future, Stick to using var instead of explicit typing unless the intent of the code would be more clear with explicit types.
- Var is preferable because not only does it reduce verbosity in your code but it also gives yourself and other developers on the team quick overview of whether the variable can be changed or not.
- Use const as much as possible (when you know the variable will never change). It can give better performance.
Conclusion
That's about it for data types in Dart. Up next is strings in Dart!
If you have any question, leave a comment or reach out to me directly and I'll do my best to help you and / or revise my post for better clarity.
Top comments (0)