DEV Community

Cover image for Variables in Dart
Jay Tillu😎
Jay Tillu😎

Posted on • Updated on

Variables in Dart

Variable declaration and its fundamentals are the basic building block of any programming language. Let’s explore a little bit about dart variables.

Variable is a named space in memory that stores value. You can say that it is a container for data.

Following are the naming rule for a variable.

  • Identifiers cannot be keywords.
  • Identifiers can contain alphabet and numbers. But identifier cannot start with a number.
  • Identifiers cannot contain white spaces and special characters except underscore (_) and the dollar ($) sign.

Variable declaration Syntax

  • In dart a variable must be declared before it is used.
  • Example of creating and initializing the variable.
var name = Akshay;
Enter fullscreen mode Exit fullscreen mode
  • Here var is a keyword used to declare a variable.
  • Dart supports a type-inference. So that here compiler will automatically know that this is a variable of type string by the initializer.
  • Here you can explicitly specify the type of a variable by prefixing it. Consider below example:
String name = "Akshay";
int highScore = 120;
Enter fullscreen mode Exit fullscreen mode

All the variables in dart store a reference to the value rather than containing a value. Here variable name contains a reference to a string object with a value of “Viveki”.

The Dynamic Type

  • Variables declared without a static type are implicitly dynamic.
  • If a variable is not restricted to a single-type, specify it as dynamic type.
dynamic score = 56;
Enter fullscreen mode Exit fullscreen mode

Default Values

All uninitialized variables have an initial value of null.

Even variables with numeric types are initially null because numbers — like everything else in dart — are objects.

void main() {
  int highScore;
  print(highScore);
}

Output
null
Enter fullscreen mode Exit fullscreen mode

That’s all you need to know about the basics of variables in the dart. Feel free to let me know if I miss something. I’ll love to learn it.

Till then Keep Loving, Keep Coding. I’ll catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

For More Information Please Visit Following Links

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

Top comments (0)