Flutter interview questions & answers part - 1
Tags: flutter, dart, interview, codenewbie
Published: 2022-09-10 10:39:44

## Introduction

In this article, I am going to talk about 10 Flutter interview questions and answers. I will be writing a series of articles on this topic. Let's get started before we make any deals.

## 1. What is Flutter?

Flutter is a cross-platform development toolkit developed by Google that enables you to build native applications for multiple platforms with a single codebase and achieve great UI quickly.

{% embed https://youtu.be/I9ceqw5Ny-4 %}

## 2. What is Dart?

Dart is an open-source general-purpose programming language. It was developed by Google in 2011 and later approved as a standard by ECMA. Dart compiles the source code similarly to other programming languages like JavaScript, but the standalone Dart SDK is shipped with a Dart VM (Virtual Machine).

## 3. What are the types of widgets in Flutter?

Basically, there are two types of widgets,
- StatefullWidget 
- StatelessWidget

## 4. What is the difference between StatefulWidget and StatelessWidget?

A StatefulWidget holds the state of widgets and can be rebuilt when the state changes. While the app is running, you can change a widget's state multiple times by calling setState(). 

A StatelessWidget, on the other hand, cannot change its state during the application's runtime, so you use it when the widget won't change.

## 5. What is the difference between hot reload and hot restart?

Hot reload simply loads your code changes into the VM (Virtual Machine) and rebuilds the widget tree with the current app states. It does not execute the main() and initState() methods again.

Hot restart loads your code changes into the VM and restarts the flutter app from the main() method. It lost the previous app state and created a new app state after restarting from the main() method.

## 6. What is the difference between runApp() and main()?

In Flutter, the main() function is used to start the program. Without it, you can't write any programs.
 
And the runApp() function is used to return the widgets that are connected to the screen as the root of the widget tree to be rendered on the screen. 

## 7. Tell me the names of apps built on Flutter.

The world's most valuable and top companies are using Flutter, The following are some of the most popular apps built on Flutter:

- Google Pay
- Google Ads
- Dream 11
- Zerodha 
- Reflectly , etc

## 8. What is the use of Mixins?

Dart does not support multiple inheritances, but Maxins gives us the ability to achieve multiple inheritances. To put it simply, mixins are classes that allow us to borrow methods and variables without extending the class. Dart uses a keyword called `with `for this purpose.

For example,
We have a Car class and we want to access Brand and Model both clasess methods in Car class at this situation mixins give us the opportunity to achieve this using the keyword `with`.


![Mixins class diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6v2ncxhhxycpfkfhfmbg.jpg)

```dart
class Brand {
  void getBrand({required String carBrand}) {
    print("Brand name : $carBrand");
  }
}

class Model {
  void getModel({required String carModel}) {
    print("Model : $carModel");
  }
}

class Car extends Brand with Model {
  void getCarDetails({required String carBrand, required String carModel}) {
    print("Hey, here is my car details");
    getBrand(carBrand: carBrand);
    getModel(carModel: carModel);
    
  }
}

void main() {
  Car car = Car();
  car.getCarDetails(carBrand: 'Tex', carModel: '1976 Cadillac Coupe DeVille');
}

```

## 9. What is the use of the pubspec.yaml file in Flutter?

In your Flutter project, the pubspec.yaml defines the dependencies. It is written in the YAML language. The pubspec.yaml file contains a number of fields, including the name, description, version, dependencies, environment, assets, and more.


## 10. What is the lifecycle of StatefullWidet?

A stateful widget has the following lifecycle stages:
- **createState()**
Every StatefulWidget calls createState() as soon as it is built, so this override must be present.

- **initState()**
This is the first method called after a Widget is created.

- **didChangeDependencies()**
This method is invoked when the widget is constructed for the first time after initState()

- **build()** 
It is called immediately after didChangeDependencies(). It is called every time the UI needs to be rendered and renders all the GUI.

- **didUpdateWidget()**
This method will be called as soon as a change is made to a parent widget that requires redrawing.

- **deactivate()**
This function is called when an object is removed from the tree. It is called before disposing.

- **dispose()**
This method is called when an object is permanently removed from the tree.

![StatefulWidget Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9yiz8vk6xgwmevyyxhv.jpeg)Image by [Jelena Jovanoski](https://jelenajjovanoski.medium.com/)

## Conclusion 

In this article, we discussed 10 Flutter interview questions and answers. Stay tuned for the next article discussing more interview questions and answers.

I hope you liked this article.