Flutter Interview Questions (2024) PART — 2

Bhawani Shankar
4 min readMay 9, 2024

--

📝 Welcome back to the Flutter interview questions series! 🎉 Get ready for more insightful questions and tips in Part 2 of our journey.

1. What is a generic type function in Dart?

A generic type function in Dart allows you to define functions that can work with any data type. It provides flexibility and reusability by allowing you to write functions without specifying the exact types they will operate on.

To implement a generic type function in Dart, you can use the syntax <T>. Here's an example:

T genericFunction<T>(T value) {
return value;
}

void main() {
// Example 1: Using generic function with a string
String stringValue = genericFunction<String>('Hello, world!');
print(stringValue); // Output: Hello, world!

// Example 2: Using generic function with an integer
int intValue = genericFunction<int>(42);
print(intValue); // Output: 42

// Example 3: Using generic function with a list
List<double> doubleList = genericFunction<List<double>>([3.14, 2.71]);
print(doubleList); // Output: [3.14, 2.71]
}

In the above example, the genericFunction takes a parameter value of type T and returns the same type T. When calling genericFunction, you specify the type you want to use within angle brackets (<T>), and the function will operate on that specific type. This allows the function to work with various data types without the need for overloading or duplicating the function for each type.

2. What is key.properties file in android?

The key.properties file in Android is a convenient way to store sensitive information, particularly signing keys, required for signing Android apps. It's typically used in conjunction with the build.gradle file to automate the signing process during app builds.

3. How can an Android app bundle be signed without using the key.properties file?

  1. By configuring signing information directly within the build.gradle file.
  2. By using Android Studio’s project settings to define the signing configuration.

4. What is dependency_overrides how it is useful in flutter?

In Flutter, the dependency_overrides parameter is a feature in the pubspec.yaml file used for overriding package dependencies

dependency_overrides can be useful when you have multiple packages in your project that depend on a single package but require different versions of that package.

To resolve this conflict, you can use dependency_overrides to specify which version of example_package each package should use:

dependency_overrides:
wakelock_plus: ^0.1.0

5. What is debouncing?

Debouncing is like pressing a snooze button. It waits for a pause before taking action. So, if there are many rapid events, it only reacts after things calm down for a bit. For example, when you’re typing in a search box, it waits for you to stop typing for a moment before performing the search, avoiding unnecessary searches for each letter you type.

6. If we are using flutter local notification then what is the default icon directory for android?

When we’re using local notifications with the flutter_local_notifications package on Android, the default icon directory is usually the drawable folder.

7. What is dependency injection in Flutter, and how can you implement it using GetIt for managing SharedPreferences?

Dependency injection in Flutter is a design pattern that allows for the separation of object creation and its usage. It helps in making code more modular, testable, and maintainable by reducing the coupling between classes.

One popular package for implementing dependency injection in Flutter is GetIt. It provides a simple yet powerful service locator for managing dependencies.

main.dart

void main() async {
GetIt locator = GetIt.instance;
final sharedPreferences = await SharedPreferences.getInstance();
locator.registerLazySingleton(() => sharedPreferences);
}

anyfile.dart

SharedPreferences? sharedPreferences;
sharedPreferences = locator<SharedPreferences>();
int? id = sharedPreferences!.getInt('id');

8. How dependency injection works in GetX?

We can create a service with extends a class with GetxService

import 'package:get/get.dart';

class TestService extends GetxService {
@override
void onInit() {
print('TestService Created');
super.onInit();
}

@override
void onClose() {
print('TestService Closed');
super.onClose();
}

void test() {
print('TestService Test');
}
}

Now we can Initialize this service with

Get.put(TestService());

And now we can find and use this service anywhere by below syntax

TestService testService = Get.find<TestService>();
testService.test();

9. How can you change the Flutter version without using the flutter downgrade or flutter upgrade command?

To change the Flutter version without using the flutter downgrade or flutter upgrade command, follow these steps:

  1. Open a terminal window and run flutter doctor -v to get the installed path of Flutter.
  2. Navigate to the Flutter installation directory using the terminal. This directory contains the Flutter SDK files.
  3. Once inside the Flutter installation directory, use the git checkout command followed by the desired version number. For example, to switch to version 1.22.6, you would run: git checkout 1.22.6.
  4. After checking out the desired version, run flutter doctor in the terminal to verify the changes and ensure that the Flutter version has been updated.

10. What will happens when we use below code if name is null?

 Visibility(
visible: name != null, // Show the Text widget only if name is not null
child: Text(
name!, // Use null-aware operators to handle null case
style: TextStyle(fontSize: 24),
),
),

It will give you an error Unexpected null value

We can resolve this error by using null aware operator

Visibility(
visible: name != null, // Show the Text widget only if name is not null
child: Text(
name ?? "Name not available", // Use null-aware operators to handle null case
style: TextStyle(fontSize: 24),
),
),

It is because text widget is present in widget tree only visibility is false

--

--