Flutter is an open-source UI software development kit (SDK) developed by Google that allows you to easily create applications for various platforms such as Android, iOS, and the web. In section 5.3 of this tutorial, we will explore how to declare variables in Flutter and understand the size of instances. Since this topic is fundamental and core to programming, let’s go through it step by step.
1. What is a variable?
A variable is a named space that can store data. In programming, variables are used to manage and manipulate data. There are several ways to declare variables in Flutter, mostly defined using the keywords var
, final
, and const
.
1.1 var
var
allows the type to be inferred automatically when declaring a variable, creating a variable whose value can be changed. For example:
void main() {
var name = 'Flutter';
print(name); // Output: Flutter
name = 'Dart'; // Can be changed
print(name); // Output: Dart
}
1.2 final
The final
keyword defines a variable that can be set only once. In other words, it cannot be changed after initialization. This helps enhance the safety of the program.
void main() {
final int age = 10;
print(age); // Output: 10
// age = 20; // Error: final variables cannot be changed.
}
1.3 const
const
declares a compile-time constant. This means the value is determined before the program runs and cannot be changed. const
is mainly used to define constant values or constant lists.
void main() {
const double pi = 3.14;
print(pi); // Output: 3.14
// pi = 3.14159; // Error: const variables cannot be changed.
}
2. What is the size of an instance?
The size of an instance refers to the amount of memory space an object occupies. This is an important factor in optimizing memory usage efficiency in applications with a lot of dynamic data structures.
In Flutter, it is very common to create and manage instances of objects. Accordingly, each instance needs to know how much memory its class’s properties and methods occupy.
2.1 Classes and Instances
A class is an essential element of object-oriented programming (OOP) that serves as a template for creating objects. A class can include properties (variables) and methods (functions). An instance is a concrete implementation of such a class.
class Person {
String name;
int age;
Person(this.name, this.age);
}
void main() {
var person1 = Person('Alice', 30);
var person2 = Person('Bob', 25);
print(person1.name); // Output: Alice
print(person2.age); // Output: 25
}
2.2 Calculating the Size of an Instance
When you want to know the size of an instance in Flutter, you can use memory diagnostic tools or analyze memory usage through development tools to check the size of the instance.
Generally, the size of an instance depends on the type and number of the class’s properties. Below are examples showing the basic size of objects in memory.
- String: 2 bytes (uses UTF-16) + number of characters
- int: 4 bytes
- double: 8 bytes
- bool: 1 byte
For example, an instance of the Person
class stores a name and an age, so it has the following memory structure:
class Person {
String name; // 2 bytes × number of characters in the name
int age; // 4 bytes
}
3. Tips for Optimization
It is important to reduce the instance size for efficient memory management. Here are some tips for optimizing instance size:
3.1 Removing Unnecessary Variables
Optimizing variables within a class can reduce memory usage and improve throughput.
3.2 Using Primitive Types
Using primitive types rather than creating new classes can help reduce instance size.
3.3 Lazy Initialization
Create instances only when needed to avoid unnecessary memory allocation. This can help reduce initial memory expenditure.
class LazyPerson {
String _name;
int _age;
LazyPerson(String name, int age) {
_name = name;
_age = age;
}
// Name getter (lazy loading)
String get name => _name;
// Age getter (similar handling possible)
}
4. Conclusion
Understanding the size of variables and instances is very important in modern mobile frameworks like Flutter. By understanding the types of variables and the size of instances and managing them properly, better memory efficiency and performance can be achieved. The content covered in this tutorial is essential when developing with Flutter, so I hope you practice to gain a deeper understanding. See you in the next topic!