Uncommon questions I ask in Flutter interviews

Chahat Gupta
4 min readJun 11, 2023

--

I hadn’t spent much time with Flutter when I was offered an opportunity to take a round-one interview for an engineering role focused on Flutter at my organisation.

For context, at that time I had about six years of experience in Android app development with Java and Kotlin, along with some backend work history.

I took the chance to ask myself — “What questions can a focused beginner answer, but an uncommitted expert can’t?”. I grabbed my pen and scribbled down these six questions:

Q1 — Discuss the difference between these operators in Dart

?
?.
??
??=

Answer

  1. ? (Ternary Operator): In Dart, the ternary operator allows you to conditionally choose between two expressions based on a boolean condition. If the condition is true, the expression on the left of : is evaluated. Else, the expression on the right of : is evaluated — and the value is returned.
  2. ?. (Conditional Member Access): Also known as the “safe navigation” or “null-aware member access” operator, it combines null-checking and member access in a concise way. It allows accessing properties or invoking methods on an object if the object is not null, otherwise the expression evaluates to null without throwing an exception.
  3. ?? (Null Coalescing): Also known as “null default”, this operator is used for null coalescing, providing a default value if the left-hand side expression is null.
  4. ??= (Null Coalescing Assignment): This operator combines the null coalescing operator (??) with assignment (=). It assigns the right-hand side expression to the left-hand side variable only if the variable is currently null. If the variable is not null, its value remains unchanged. It works great for singleton creation.

Q2 — What are these operators used for in Dart?

.
..
...

Answer

  1. . (Dot Operator): The dot operator is used for accessing members (properties and methods) of an object or class.
  2. .. (Cascade Operator): The cascade operator allows you to perform multiple operations on the same object in a cascading manner. It is used to chain multiple method calls or property assignments on a single object without repeating the object reference.
  3. ... (Spread Operator): The spread operator is used to spread the elements of an iterable (such as a list or set) into another collection or function call. It unpacks the elements of an iterable into another iterable or as arguments for a function call that accepts multiple arguments.

Q3 — If you pass a StatefulWidget as a child of a StatelessWidget, does the StatefulWidget lose its stateful behaviour or the StatelessWidget gains state?

SomeStatelessWidget(
child: SomeStatefulWidget()
)

Answer

If you pass a StatefulWidget as a child of a StatelessWidget in Flutter, the StatefulWidget does not lose its stateful behaviour. However, the state of the StatefulWidget will not be preserved or managed by the parent StatelessWidget because StatelessWidget does not have an associated state.

In this scenario, the StatefulWidget will still maintain its own internal state management. The stateful widget will create and manage its own State object, which holds the mutable state data and handles state changes.

Q4 — Are functions and methods the same?

This isn’t a Dart or Flutter specific question. It is a general OOP question that I like to shoot randomly.

Answer

Functions are blocks of code that can exist anywhere can be executed independently. They can have parameters and return types and can be made globally accessible within the app or program. Examples include main() and print() functions in Dart.

On the other hand, methods are functions associated to an object or a class. They are declared inside a class and can interact with or modify the object they belong to. In OOP (Object-Oriented Programming), methods can be considered as a subset of functions. Some examples are toString() and compareTo() methods.

All methods are functions, but all functions are not methods.

Q5 — What is the difference between using var and dynamic while declaring variables in Dart?

// declaration using var
var a = 10;

// declaration using dynamic
dynamic a = 10;

Answer

The var and dynamic keywords in Dart are used to declare variables with different characteristics. The var keyword is used for declaring variables when the type can be inferred from the assigned value. When using var , the type of the variable is determined at compile-time based on the assigned value. Once the type is inferred, it becomes fixed and cannot be changed.

However, The dynamic keyword is used for declaring variables when the type can change. It provides flexibility but sacrifices some of the type safety provided by static type checking in Dart.

Q6 — Explain the difference between final and const in Dart.

In Dart, both final and const are used to declare variables with values that cannot be changed.

However, the main difference between the two is that the value of a final variable is determined at runtime when it is assigned, whereas the value of a const variable must be known at compile-time.

When we declare a const variable in a class, its value remains the same across all instances of the class. On the other hand, the value of a final variable may change across instances. Additionally, it’s important to note that all const variables are implicitly static.

Conclusion

These are some questions that I believe give a good understanding of the candidate’s basic knowledge of the language. Everybody gets better with UI and logic with time and practice, but not all understand and explore the foundational concepts.

Knowing the answers to these questions may or may not help you with the interview, but they will definitely make you a better programmer. Let’s meet in another blog. Happy coding!

--

--

Chahat Gupta
Chahat Gupta

Written by Chahat Gupta

Mobile Tech Lead specialising in Android, iOS, and Flutter. Sharing insights and learnings on mobile development to inspire and elevate tech professionals.