DEV Community

Cover image for Dart - Variables & Challenges
SinachPat
SinachPat

Posted on

Dart - Variables & Challenges

This is the beginning of a series on Dart/Flutter that will have me sharing bits of Dart/Flutter every now and then on the fundamentals of building cross-platform applications using the language and framework. The purpose of this is to provide a summarized reference for the Flutter Roadmap which shows a path for learning Flutter towards mastery. I’ll provide this content with well-commented codes so beginners will be able to make references to these resources and not just learn about the Dart/Flutter language but really understand how it works.

Every programming language has a way of storing values, objects and methods. In Dart, variables are a way of storing values. These values can be called References. You can always use the value of the variable by calling on the variable.

Variables in Dart are defined with the type clearly indicated as String, Object, Int, bool etc.

// Here a variable is defined with name as myFirstName which stores 

// my first name which is Osinachi 

String myFirstName = "Osinachi";
Enter fullscreen mode Exit fullscreen mode
bool confirmName = true;
Enter fullscreen mode Exit fullscreen mode

You can use the variable type Object for variables that can have changing values. For example you can define a value to be a String and later change it to be an Int depending on the flow of the program.

When defining a variable which value can be Null, the code should be able to show the bear a question mark which ensures that the code doesn’t throw an error when used later in the program and a state in the program assigns it a null value. The format for defining a nullable value in Dart/Flutter is shown below.

int? age;
//the assert here confirms that this variable can be null throughout the program
assert(age == null);
Enter fullscreen mode Exit fullscreen mode
String? chainName;
//the assert here confirms that this variable can be null throughout the program
assert(chainName == null);
Enter fullscreen mode Exit fullscreen mode

Late Variables

If you declare a non-nullable variable and wish not to assign in a value at the initial declaration, the late modifier is used to tell the program “Hey, this is a value store in this program. I would be using it later to store a value so just keep it for me as late.”

In programming, variables are used to store values or objects. In Dart, variables are defined with the type clearly indicated as String, Object, Int, bool, and others. When you declare a variable, you must assign a value to it immediately. However, there are instances when you need to declare a variable but do not wish to assign a value to it immediately. This is where the late modifier comes in handy.

The late modifier is used to defer the initialisation of a non-nullable variable until it is accessed for the first time. This is useful when you need to declare a variable that will be assigned a value later in the program, but you don't want to initialise it with a dummy value at the time of declaration. This can help to make your code more efficient and easier to read.

For example, if you have a variable that will be initialised with the result of an asynchronous operation, you can declare it as a late variable to avoid having to initialise it with a default value that will be immediately replaced. Instead, you can simply declare it as late and assign a value to it later in the program. This can help to reduce clutter in your code and make it easier to understand.

It's worth noting that late variables must be initialised before they are accessed, or an exception will be thrown. This means that you should be careful when using late variables to ensure that they are initialised correctly and in a timely manner. If you attempt to access a late variable before it has been initialised, your program will throw an exception, which can cause your program to crash or behave unexpectedly.

late String nameHint;

//Here the variable is now initialized and assigned a value.

void main() {
String nameHint = "Os";
}
Enter fullscreen mode Exit fullscreen mode

While this is for variable declarations you would want to change later, there are variables we use in our program that we wish to declare just once and not change again. We would talk about constants and finals in the next section.

Const and Final Variables

You can define variables you don’t want to change throughout the program with the keywords final or const. This stores values permanently in a program that can’t be changed anywhere in the program.

const String nameHint = "Os";
final String fullName = "Osinachi Patrick"
Enter fullscreen mode Exit fullscreen mode

You can use final in Instance Variables (variables declared within a class) for representing values that can’t change.

class Person {
  String? firstName; // Declare instance variable x, initially null.
  String? lastName; // Declare y, initially null.
  final int age = 21; // Declare z, initially 0.
}
Enter fullscreen mode Exit fullscreen mode

In summary, we use variables in many instances in our programs and it’s important we understand how it works fundamentally.

To understand the concept of variables more deeply, let’s have some challenges to work on.

Challenge for the course

See you in my next article 💙

Top comments (0)