Dart Data Types

| Page Views: 97

Dart Data Types and Variables example.

Working with Literals

Here's an example indicating how to work with string, float, integer and boolean literals.

main() {
  // You can use the `+` to concatenate strings
  print("This is string " + "concatenation");
  print("Hello " + "World");

  // You can also interpolate values within a string.
  // Add two integers
  print("108 + 112 = ${ 108 + 112 }");
  // Divide two Floats
  print("234.23 / 31.09 = ${ 234.23 / 31.09 }");

  // Boolean Values
  print(true && false);
  print(false || true);

The above will result in:

This is string concatenation
Hello World
108 + 112 = 220
234.23 / 31.09 = 7.533933740752653

Variable Declaration using var keyword

In Dart you can declare variables using the var keyword:

main() {
  var name = "Albert Einstein";

  var age=76;

  var balance = 1200.50;

  var isMarried = true;


Here's the result:

Albert Einstein

In the above we've declared both an integer and a string without specifying the type. Dart will then use Type Inference to determine the type for us:

Getting the Type of a given Variable

Sometimes you want to determine the type of a given variable. In that case you can use the runtimeType property of the Type class.

Here's an example:

main() {
  var name = "Albert Einstein";
  print(name + " is a " + name.runtimeType.toString());

  var age=76;
  print(age.toString() + " is an " + age.runtimeType.toString());

  var balance = 1200.50;
  print(balance.toString() + " is a " + balance.runtimeType.toString());

  var isMarried = true;
  print(isMarried.toString() + " is a " + isMarried.runtimeType.toString());


And we get:

Albert Einstein is a String
76 is a int
1200.5 is a double
true is a bool

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post