Let’s Dive into Dart: The Language for Crafting Stunning Flutter Apps
Hey developers!👋👨💻👩💻
📱🖥️🌐🖥️📱 Building applications for multiple platforms can be challenging due to the learning curve of needing to learn multiple languages and frameworks. 😫 But with Flutter, you can 🚀✨✍️ deploy to multiple devices and platforms like Android, iOS, web, desktop, and embedded systems 📲💻🖥️📟 from one Flutter code base written in one language, Dart. 🙌🎯🚀
Before we begin our journey with Flutter let me tell you a bit about the Dart programming language and how it may flow from your other programming languages. we will go through the most important concepts of the Dart programming language that is required for you to start with Flutter. And one of the first things we need to know about is the entry point in a Dart application.
If your coming from a languages like C++, or Go, or even Java, you must be aware of the top level function called Main. This is where your application starts executing.
void main(){
print("I am loving Dart");
}
If you have ever worked with any other programming language then you are aware of variables and constants, and that there are different types of data that you can store such as numbers, strings, characters, etc. so I won’t be going into details of what variables and constants are because I expect you have some experience. But I will be discussing the different data types that are available in Dart.
Dart Supports Nine Data Types
The Dart language has built in support for nine data types,
- Numbers
- Strings
- Booleans
- Lists
- Maps
- Sets
- Runes
- Symbols
- The value “null”
I just care about 5 data types ( Numbers, Strings, Booleans, Lists, Maps) and this will be enough for us to get started with Flutter.
Let’s start with the very first one 📌👉🚀.
1.Numbers (Numbers like 3456, 900, 5.5)
Basically just numbers like 3456, 900, 5.5 , etc. Now in Dart, there are usually two types of numbers.
- Integers (Integer values or whole numbers)
int someNumber = 55, 700
2. Floating point numbers ( Floating point numbers or numbers with decimals)
double someDouble = 5.5
In case you’re having to store some data which could either be a whole number or a decimal number , then you can use the third type, which is num which can store both int or double values.
void main(){
int someNum = 7;
print(someNum.isEven);
double someDouble = 5.6;
print(someDouble.round());
}
When you’re working with int, or double, or even num, Dart provides some default methods and properties that you can make use of in your use cases, such as if you want to check, if the given integer number is an odd number or an even number or maybe you want to round up a double number and convert it into an integer, so here you can use the round() method. To know more about these default properties and methods you can head over to api.dart.dev and search for the data type that you want to learn about.
Let’s move on to second one 📌👉,
2.Strings
You must have worked with Strings in all of the programming languages that you have worked with , and in Dart, it is no different. Strings are declared with a data type named ‘Sting’ with a capital S, and is used to represent a sequence of characters.
The only difference might be how a string takes in a variable in its value, so let’s move into the example.
void main(){
int appleCount = 5;
String msg = "I have $appleCount apples";
print(msg);
}
This method of adding only works, if you’re trying to add an identifier or a variable like this, in case of an expression like ‘appleCount.toString’ it is not recognized anymore. In that case you can use curly braces and wrap that expression within it.
void main(){
int appleCount = 5;
String msg = "I have ${appleCount.toString()} apples";
print(msg);
}
So if you are using an object of a class this also works there, so if it’s an identifier or a variable then you can simply use a dollar sign ($) but otherwise you can use the curly braces, and wrap the expression within it.
you can also do this,
void main(){
int appleCount = 5;
String msg = "I have" + appleCount.toString() + "apples";
print(msg);
}
like where you have the string value and then you use the plus sign (+) , remove all the dollar signs and extras here, and then add the inverted commas for just the string parts. This also possible but this does not seem too readable anymore. The other method of string concatenation is more recommended.
The string class also comes with a lot of useful methods to manipulate the string values. such as,
print('Never odd or even' .contains('odd'));
if you want to check if this particular string contains any word called ‘odd’, or
print('Never odd or even' .startWith('Never'));
does this string start with the word ‘never’, or
print('hello world' .toUpperCase());
can we convert this string to an Upper case, or
print('' .isEmpty);
checking if this string is empty or not.
As mentioned earlier you can find the entire list of methods and propertes in this website: api.dat.dev
Let’s move on to more data types 📌👉🔤🔢
3.Booleans (true or false)
The next one being Booleans, which can hold only two values, one is true and another is false.
Suppose you created a variable called ‘isLocationEnabled’ to check if location services are enabled for a certain device or not. Now you want to check conditions such as, if location services are enabled for a certain device, then only check the current GPS location. And when is is false, then you just print an error message like “Location services disabled”.
bool isLocationEnabled = false;
if(isLocationEnabled)
checkCurrentLocation();
else
return "Location services disabled";
Also instead of explicitly mentioning ‘== ‘to true , you can omit it, and the compiler will understand that you want to check if this is true. For false, you can just add an exclamation mark (!) at the beginning and you’re done.
bool isLocationEnabled = false;
if(!isLocationEnabled)
checkCurrentLocation();
else
return "Location services disabled";
Now let’s move on to the next type 📌👉,
4. List
It is similar to Arrays and some other programming languages. Here a list is basically just an ordered list of the same type or different types, like a list of books or a list of item prices.
List bookTitles = ["Harry Potter", "Twilight", "XYZ"];
List prices = [300.4, 600, 6494, 0, 400.55];
void main(){
List bookTitles = ["Harry Potter", "Twilight", "XYZ"];
print(bookTitles);
}
Here is a simple list that is initially assigned with a list of bool names like Harry Potter, And we can simply print this and get the list in the console. But what is the data type that this list is supposed to store?
If you want to check the runtime type of the list, you can just add “bookTitles.runtimeType”. And this will shows that it is a JSArray of dynamic ( JSArray<dynamic>), which is basically, you can see that it’s a list of dynamic. Dynamic simply means that this can hold a value of any data type.
void main(){
List bookTitles = ["Harry Potter", "Twilight", "XYZ"];
bookTitles.add(2);
print(bookTitles.runtimeType);
}
Suppose bookTitles.add(2), this will not throw an error because this allowed.
Now, if you want to restrict a data type, meaning you only want string values to be added to a list, then you can simply write the concern data type with an angled bracket here. So in this case, i can make it a String.
void main(){
List<String> bookTitles = ["Harry Potter", "Twilight", "XYZ"];
bookTitles.add(2);
print(bookTitles.runtimeType);
}
Now the compiler will throw errors that a number is not allowed to be added to a list that allows only string. This is because something called generics.
Similarly to previous datatypes Dart also provides a number of methods to modify or manipulate a list, like finding the length of a declared list.
void main(){
List<String> bookTitles = ["Harry Potter", "Twilight", "XYZ"];
bookTitles.add(2);
print(bookTitles.length);
}
So in this case, if I want to check the length of book titles, then i can do just ‘bookTitles.length’ and this should give me three.
And what if I want to clear out the list contents?
void main(){
List<String> bookTitles = ["Harry Potter", "Twilight", "XYZ"];
bookTitles.clear();
print(bookTitles.length);
}
so in this case ,I can just do ‘bookTitles.clear’ and then if I put in the length, there should be zero afterwards.
Now let’s talk about the final data type 📌👉,
5.Maps
we must know about which is maps. Map objects are surrounded by curly braces containing a list of data with each item having a key and a value.
Map employeeList = { 001: "John Doe", 002: "Jane Doe"}
| |
Key Value
This is useful for storing values that comes with some identifier of sorts, such as employee ID mapped with employee name.
Maps are basically like a key value pair. Like if you have ever worked with JSON, then you do identify that it is a map of string and dynamic.
Similar to list, the type of this map will be dynamic and dynamic ( Map<dynamic, dynamic) since no data type has been defined while creating this variable.
But just like maps, we can define the data type if we want the type to be strict, such as creating a map of int and string and having no other data type allowed.
So here we can have int and string.
void main(){
List<String> bookTitles = ["Harry Potter", "Twilight", "XYZ"];
Map<int, String> employeeList = {001: "John Doe", 002: "Jane Doe"};
}
So that way , if i add a map of int and int, its should not be allowed. Technically, this should throw a compiler error because in the second one, it is supposed to be a string, but here we are providing an integer.
void main(){
List<String> bookTitles = ["Harry Potter", "Twilight", "XYZ"];
Map<int, String> employeeList = {001: "John Doe", 002: "Jane Doe"};
employeeList.addAll({'2:2'});
}
Now all the variables that we have defined till now were given a value, but in real world use case, sometimes this value can also be null. But if we try to reassign this variable with a null value, it will throw an error. So does that mean non of these variables can ever hold a null values? This would be a problem because sometimes a null value is pretty important in certain cases. So obviously, Dart does provide you a way to create nullable variables, If you simply add a question mark after writing the data type.
void main(){
List<String?> bookTitles = ["Harry Potter", "Twilight", "XYZ"];
Map<int, String?> employeeList = {001: "John Doe", 002: "Jane Doe"};
employeeList = null;
}
Then the Dart compiler assumes that this variable can hold a value or a null object.
Like maybe for this map, you can also have a use case where the employee name can be null. So here, you can add like a question mark just after string as well. So that way , not only this map can be null, but you also can have an employee list value, where you can have an integer, which cannot be null but the employee name, which is a string, that can be null. So in this case, you have 2 or 003, and null.
void main(){
List<String?> bookTitles = ["Harry Potter", "Twilight", "XYZ"];
Map<int, String?>? employeeList = {001: "John Doe", 002: "Jane Doe"};
employeeList.addAll({003: null});
}
So yeah, the employee does not have a name or we do not have the record of the employee yet. So this is also now allowed.
And since Dart 2.12, all the variables defined in Dart are non nullable by default. So the only way to make them nullable is to add this question mark(?) during declaration.
We will discover more about null saftey concepts. Now that’s all the data types that we need to know before starting our Flutter journey. There are obviously more types but you can learn them as you advance in your learning.
But there’s also another concept that you must be aware of, which is access modifiers. Not all variable should be accessible outside of the class or method or library. In Java, we have some keywords like public, private and protected. Dart doesn’t provide those kind of keywords. Instead , you can use the underscore (_) at the start of the name of an identifier oe a data member of a class to make it private.
Anyway, this gives you enough idea on data types in Dart and now you can easily work with data for your Flutter applications💡📝🎯.
Happy coding! 😄💻🎉