Câu hỏi phỏng vấn Flutter
Câu hỏi

What is Fat Arrow Notation in Dart and when do you use it?

Câu trả lời

The fat arrow notation (=>) in Dart is a shorthand syntax for defining single-expression functions. It allows you to write concise function expressions in a single line of code[1].

The syntax for using the fat arrow notation is:

dart Copy
ReturnType functionName(parameters) => expression;

Here's how it works:

  • The ReturnType specifies the return type of the function, such as void, int, bool, etc.
  • functionName is the name of the function.
  • parameters represents the parameters the function requires, if any.
  • expression is the single expression to be evaluated and returned by the function.

The fat arrow notation is particularly useful when you have a function that performs a simple operation and returns a value in a single line of code. For example:

dart Copy
bool isEven(int number) => number % 2 == 0;

In this case, the isEven function checks if a given number is even and returns a boolean value.

You should use the fat arrow notation when:

  1. **The function body consists...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

How is InheritedWidget different from Provider ?

junior

When should you use WidgetsBindingObserver ?

senior

What is a difference between these operators ?? and ?.

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào