Flutter Interview Questions (2024) PART — 1
👋 Hey! I’ve got some helpful Flutter interview questions to share for your next job opportunity. 🚀 Let’s dive in! 😊
1. What is Singleton Class in Dart?
A Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to it. To implement a Singleton in Dart, you can use the factory
constructor.
If you create two objects, s1
and s2
, from the Singleton
class and subsequently modify an attribute of the s1
instance, the change will be reflected in the s2
instance as well, like below example.
class Singleton {
int data = 1;
// Private static instance of the Singleton class
static final Singleton _singleton = Singleton._internal();
// Private constructor that can only be called from within the class
factory Singleton() {
return _singleton;
}
// Private named constructor used to initialize the singleton instance
Singleton._internal();
}
void main() {
// Creating two instances of the Singleton class
var s1 = Singleton();
var s2 = Singleton();
// Checking if s1 and s2 reference the same object (singleton)
print(identical(s1, s2)); // true
// Checking if s1 and s2 are equal (will be true because they reference the same object)
print(s1 == s2);
// Accessing and printing the 'data' property of s1 and s2
print(s1.data);
print(s2.data);
// Modifying the 'data' property of s1
s1.data = 2;
// Printing the 'data' property of s2 to see that it reflects the change made to s1
print(s2.data);
}
OUTPUT:
true
true
1
1
2
If you are not using the Singleton pattern, each object created from the class will be independent of each other, and changes made to one object will not affect the others, like below example.
class NonSingleton {
int data = 1;
}
void main() {
// Creating two instances of the NonSingleton class
var s1 = NonSingleton();
var s2 = NonSingleton();
// Checking if s1 and s2 reference the same object (non singleton)
print(identical(s1, s2)); // true
// Checking if s1 and s2 are equal (will be true because they reference the same object)
print(s1 == s2);
// Accessing and printing the 'data' property of s1 and s2
print(s1.data);
print(s2.data);
// Modifying the 'data' property of s1
s1.data = 2;
// Printing the 'data' property of s2 to see that it does not reflects the change made to s1
print(s2.data);
}
OUTPUT:
true
true
1
1
1
2. What is an Analysis Option File?
An analysis option file (analysis_options.yaml
) is used in Flutter projects to configure Dart’s static analysis. It allows developers to customize lint rules, enable or disable specific checks, and configure code style preferences. This file plays a crucial role in maintaining code quality and consistency across the project.
3. How to Get Bytes from an Assets File?
To read bytes from an assets file in Flutter, you can use the rootBundle
from the flutter/services.dart
package. The load
method allows you to load and access the content of the asset as bytes.
import 'package:flutter/services.dart' show rootBundle;
Future<void> readAssetBytes() async {
ByteData data = await rootBundle.load('assets/my_file.bin');
List<int> bytes = data.buffer.asUint8List();
// Use 'bytes' as needed
}
4. What is Split per ABI in Dart?
Split per ABI (Application Binary Interface) is a feature in Flutter that allows you to generate separate APKs for each target architecture (e.g., ARM, ARM64, x86). This can help reduce the size of the APK and improve performance on specific devices. You can use the below command to generate build for separate architectures.
flutter build apk --release --split-per-abi
5. What’s the Command for Taking a Screenshot in Flutter?
The flutter screenshot
command allows you to capture screenshots of your Flutter app running on a device or emulator or simulator.
6. What is BuildContext?
BuildContext
represents the location of a widget in the widget tree and helps us to find the location of a widget in the widget tree. Understanding BuildContext
is crucial for building Flutter applications, especially when dealing with navigation, state management, and accessing resources within the widget tree.
7. What is the difference between runApp() and main()?
main()
is the entry point of any Dart or Flutter application. It is the first function that gets executed when the application starts.
runApp()
is a function provided by the Flutter framework. Its primary purpose is to take the root widget of your Flutter application and attach it to the screen.
8. What is profile mode?
In Flutter, there is a profiling concept that allows you to analyze the performance of your app.
There are three modes in Flutter: debug, profile and release.
The profile mode works on a physical device because emulator or simulator cannot capture the accurate performance of the app.
9. How can we regenerate platform-specific folders if they have been accidentally deleted?
Below command can regenerate the platform-specific folder like android, ios, web, linux etc.
flutter create .
10. What is value equality and how we can achieve value equality in Dart?
If two objects of a class have same values then it is called value equality.
To achieve value equality, you have to override the ==
operator and the hashCode
getter in your class. This allows you to define custom rules.
Example:
class Point {
final int x;
final int y;
Point(this.x, this.y);
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Point && other.x == x && other.y == y;
}
@override
int get hashCode => Object.hash(x, y);
}
void main() {
var point1 = Point(1, 2);
var point2 = Point(1, 2);
print(point1 == point2);
}
OUTPUT:
true
Let us know how many answers you already have. If you have any questions to share, please leave them in the comments section below.”
Ready to share apps the easy way? Check out WhiteCodel App Share at tools.whitecodel.com/app-share today!