Types & Operations in Dart

fluttertanzania

Flutter Tanzania

Posted on October 14, 2023

Types & Operations in Dart

Life is full of types, in every thing we do we always use types. What kind of shoes do you wear? Boot? Sneakers? or what kind of phone do you use? iPhone? Google pixel?.
With types we're able to differentiate and categories different things.

This is the same as in computer, in programming types are useful same as in life, They help you to categorize all the different kinds of data you use in your code.

Data types in Dart
In programming types tell our compliers how we want to use data. There are many types of data in Dart and so far we have been able to use some of them including.

  • int
  • double
  • num
  • String and
  • dynamic

By using data types dart helps you to perform wrong operation in data example tying to minus a String and int.

Type inference
In our previous article we briefly looked at Type inference in dart, so get deep into it.

One of the best practise while defining a variable is by specifying it's data type like the followings:

int myAge = 22;
double random = 0.1;
Enter fullscreen mode Exit fullscreen mode

With the above code we have defined two variables and we have annotated with int and double respectively.

Making variable constants
In our above example of variables we make them mutable which means they can change within it's life time, but if we do not want that, which means making them immutable we can do that while still keeping the type annotation.

const int myAge = 22;
const double random = 0.1;
Enter fullscreen mode Exit fullscreen mode

or

final int myAge = 22;
final double random = 0.1;
Enter fullscreen mode Exit fullscreen mode

But it's required to define data type while creating a variable, Dart is smart enough to know which type a variable is using. Through type inference Dart is able to identify a variable data type. See the following code.

const myAge = 22;
const random = 0.1;
Enter fullscreen mode Exit fullscreen mode

In this code we did not tell Dart what type of data we want our variable to have, but dart will know that myAge is an int and random is double, this is called type inference.

You can run the following to see your variable data type

print(myAge.runtimeType);
print(random.runtimeType);
Enter fullscreen mode Exit fullscreen mode

Converting data type
While developing your program you may encounter yourself in situation where you want to convert a certain data to another data type.

We have seen that in Dart if we try to perform the following, Dart will complain.

const myAge = 22;
const random = 0.1;

myAge = 0.22;
Enter fullscreen mode Exit fullscreen mode

Dart will say A value of type 'int' can't be assigned to a variable of type 'double'

As we see Dart is very sensitive in Data type though other programming languages will allow you to do the above without any error. Experience shows this kind of silent, implicit conversion is a frequent source of software bugs and often hurts code performance. Dart disallows you from assigning a value of one type to another and avoids these issues.

But this does not mean that Dart will disallows you from changing data type. We can do so by doing the following.

const myAge = 22;
const random = 4.1;

myAge = random.toInt();
Enter fullscreen mode Exit fullscreen mode

The assignment now tells Dart, unequivocally, that you want to convert from the original type, double, to the new type, int.

Mixed types operations

You'll always encounter in a situation where you want to perform an operation between two different data types. Example you want to make addition between 3 + 3.3, dart is smart enough that it will return double since making it an int could cause a loss of precision.

But what if you want a result to be an int?, we you can do it by using type conversion for example;

const myAge = 22;
const random = 4.1;

final result = (myAge + random).toInt();
Enter fullscreen mode Exit fullscreen mode

This time we used final and not const because toInt() is a runtime method, which means variable result is not known during compile time which makes it invalid

Strings in Dart

In dart we can work with text with a data type known as String, now let's learn about String data type.

We have seen String in previous chapters when we printed.

print('Hello Dart world');
Enter fullscreen mode Exit fullscreen mode

Now let's define a variable and assign a string data type.

var hello = 'Hello Dart world';
print(hello);
Enter fullscreen mode Exit fullscreen mode

Even if we did not defined a data type but still Dart will assign our variable as a String.

But we can be more specific by defining it's data type, like so.

const String hello = "Hello Dart";
Enter fullscreen mode Exit fullscreen mode

Concatenation
There's a point when you want to perform a certain thing within you're String, well dart knowns that and it allow's us to do it.

var hello = 'Hello' + ' Dart ';
const anotherHello = 'World';
hello += anotherHello;
Enter fullscreen mode Exit fullscreen mode

If you print hello variable you will see Hello Dart World, here dart concatenates our Strings to be a single text.

This is very useful when you have a String in a variable and want to combine it with another String.

You can also build up a string by using interpolation, which is a special Dart syntax that lets you build a string in a manner that’s easy for other people reading your code to understand:

const String language = 'Dart';
const String hello = 'Hello $language';
Enter fullscreen mode Exit fullscreen mode

This code does the same thing like the previous one when we used + to join a text, but this more readable than the previous one

Multi-line strings

When you have a String with multiple line you can easily display as following

const string = '''
You can have a string
that contains multiple
lines
by
doing this.''';
print(string);
Enter fullscreen mode Exit fullscreen mode

The three single-quotes (''') signify that this is a multi-line string, but you can also use (""") and it will do the same thing.

Conclusion
This is just an introduction to types, you should search for more resources to learn more.

💖 💪 🙅 🚩
fluttertanzania
Flutter Tanzania

Posted on October 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related