Flutter Interview Questions (2024) PART — 3

Bhawani Shankar
6 min readJun 9, 2024

--

Welcome to the third installment of our Flutter Interview Questions series for 2024! 🚀 In this part, we’ll dive into advanced topics and concepts essential for any Flutter developer to master. Whether you’re preparing for a job interview or just looking to deepen your understanding of Flutter, these questions and answers will help you get there. Let’s get started! 🎉

1. What is SSL Pinning and why is it important?

SSL Pinning is a security technique used to ensure that an app communicates only with a trusted server by using a specific SSL certificate. This method helps prevent man-in-the-middle attacks, where an attacker intercepts and potentially alters the communication between the app and the server. In Flutter, SSL Pinning can be implemented using libraries such as http and dio. These libraries allow developers to manually handle certificate validation by checking the server's certificate against a pre-defined list of trusted certificates. If the server's certificate does not match any of the pinned certificates, the connection is rejected.

2. What is Skia in the context of Flutter?

Skia is a 2D graphics library used by Flutter to render its UI. It serves as the low-level graphics engine responsible for drawing text, shapes, and images on the screen. Skia ensures that Flutter can deliver high-performance graphics consistently across different platforms, including iOS, Android, and the web. By leveraging Skia, Flutter can provide smooth and responsive UIs with rich visual effects and animations, contributing significantly to the overall user experience.

3. How many types of streams are available in Flutter and what are they?

In Flutter, there are two main types of streams:

  • Single Subscription Streams: These streams can have only one listener at a time. They are suitable for tasks that emit a sequence of events over time and then complete, such as reading data from a file or making a network request. Once the listener is done, the stream cannot be listened to again.
  • Broadcast Streams: These streams can have multiple listeners simultaneously. They are suitable for tasks that continuously emit events and need to be listened to by multiple subscribers, such as handling mouse events or receiving updates from a web socket connection. Broadcast streams do not buffer events for late subscribers.

4. What is an Event Channel in Flutter?

An Event Channel in Flutter is used for communicating between Dart and the platform (iOS or Android) for continuous streams of data. It allows Flutter to receive asynchronous data from platform-specific APIs, such as sensor readings or connectivity changes. Event Channels enable the platform to send events to Flutter code, which can then process these events in real time, providing a seamless integration between Flutter and native functionalities.

5. What is the difference between var and dynamic in Flutter?

var: When a variable is declared with var, the type is inferred by the compiler at compile-time based on the assigned value. Once the type is determined, it cannot be changed. For example:

var name = "Flutter"; // Inferred as String
// name = 123; // Error: A value of type 'int' can't be assigned to a variable of type 'String'.

dynamic: When a variable is declared with dynamic, the type is determined at runtime, and it can change over time. This provides more flexibility but at the cost of type safety. For example:

dynamic variable = "Flutter";
variable = 123; // No error, type changes to int at runtime

6. What is the difference between final and const in Flutter?

final: A final variable can be set only once, but it can be initialized later in the code. It is used for runtime constants, meaning the value can be determined at runtime. For example:

final currentTime = DateTime.now(); // Initialized at runtime

const: A const variable is a compile-time constant, meaning its value must be provided when the variable is declared, and it cannot change. It is implicitly final, meaning it can only be set once. For example:

const pi = 3.14159; // Compile-time constant

7. What are the benefits of Dependency Injection in Flutter?

Dependency Injection (DI) offers several benefits in Flutter development:

  • Improved Modularity: DI decouples the creation of a service from its use, making the codebase more modular and easier to manage. Each component can be developed and tested independently, enhancing overall code organization.
  • Enhanced Testability: DI makes unit testing easier by allowing dependencies to be easily mocked or stubbed. This leads to more robust and reliable tests, as components can be tested in isolation with controlled dependencies.
  • Easier Maintenance: DI simplifies the process of changing dependencies without affecting the consumers of those dependencies. This makes it easier to refactor and update the codebase, as changes in one part of the system do not ripple through the entire codebase.

8. What are Generators in Dart/Flutter?

Generators are functions that produce a sequence of values. They allow you to generate values lazily, meaning the values are produced on-demand rather than all at once. There are two types of generators in Dart:

Synchronous Generators: Use the sync* keyword and yield to return an iterable sequence of values. For example:

Iterable<int> syncGenerator() sync* {
yield 1;
yield 2;
yield 3;
}

Asynchronous Generators: Use the async* keyword and yield to return a stream of values asynchronously. For example:

Stream<int> asyncGenerator() async* {
yield 1;
await Future.delayed(Duration(seconds: 1));
yield 2;
await Future.delayed(Duration(seconds: 1));
yield 3;
}

9. How do you cancel or close a stream in Flutter?

To cancel or close a stream in Flutter, you can use the StreamSubscription's cancel method for active subscriptions or call the close method on a StreamController to close the stream. For example:

// Canceling a subscription
StreamSubscription subscription = stream.listen((data) {
print(data);
});
subscription.cancel(); // Cancels the subscription

// Closing a StreamController
StreamController controller = StreamController();
controller.close(); // Closes the stream

10. What is the difference between WidgetApp and MaterialApp in Flutter?

WidgetApp: A lower-level widget that is used to create an application with basic routing support but without any Material Design features. It is suitable for apps that do not require Material Design components or theming.

WidgetApp(
home: HomePage(),
// Other properties
);

MaterialApp: A higher-level widget that provides Material Design components and themes, making it easier to build apps that follow the Material Design guidelines. It includes built-in support for navigation, theming, and more.

MaterialApp(
home: HomePage(),
theme: ThemeData(
primarySwatch: Colors.blue,
),
// Other properties
);

11. What is the difference between the Flutter main method and runApp?

main Method: The entry point of the Flutter application. It is where the execution of the program starts. You typically perform initial setup and call runApp within this method.

runApp Function: Called within the main method to inflate the given widget and attach it to the screen. It takes a Widget as an argument and sets it as the root of the widget.

12. What is Tree Shaking in Flutter?

Tree Shaking is a technique used to remove dead code (unused code) from the final binary during the build process. This helps in reducing the app size and improving performance by including only the necessary code. Tree Shaking analyzes the code and eliminates any code paths that are not reachable or used, ensuring that the final build is optimized for size and efficiency.

13. What is WebAssembly (Wasm) and how is it relevant to Flutter?

WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It allows code written in various languages to run on the web at near-native speed. While Flutter primarily targets mobile and web through Dart and its compilation strategies, the potential for leveraging Wasm could enhance performance for Flutter web applications in the future. Wasm provides a way to run compute-intensive tasks efficiently in a web environment, which can be beneficial for performance-critical Flutter web apps.

We hope this set of advanced Flutter interview questions helps you prepare for your next technical interview. Stay tuned for more parts in this series, and happy coding! 💻✨

--

--