Skip to content

Latest commit

 

History

History
494 lines (296 loc) · 28.7 KB

about_flutter.md

File metadata and controls

494 lines (296 loc) · 28.7 KB

Flutter Frequently Questions and Answers

Questions

1. What is Flutter?

2. What are the advantages of using Flutter?

3. What is the Flutter architecture?

4. What build modes are available in Flutter?

5. What is the Dart programming language?

6. Is Dart language necessary for Flutter?

7. What are widgets in Flutter?

8. What are the most useful editors for Flutter apps?

9. What are packages and plugins in Flutter?

10. Are there any popular apps which make use of Flutter?

11. In What technology is Flutter built?

12.What is the App state?

13. What do you mean by keys in flutter? and its uses

14. What is Flutter Inspector?

15. Different types of streams in Dart

16. What is the difference between SizedBox and Container?

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

18. How many types of widgets are there in Flutter?

19. When should you use WidgetsBindingObserver?

20. What is Dart and why does Flutter use it?

21. How to define a generic extension in Dart?

22. How do you create a factory?

23. List the responsibilities of FlutterActivity?

24. What is Build context?

25. How will you declare async function as a variable in Dart?

26. Why do we need Mixins?

27. Explain Ticker in Flutter?

28. Explain Stream in Flutter?

29. Explain Null aware pointers?

30. How can you run code in debug mode only?

31. What is Provider?

32. What are the limitations of Flutter?

33. Explain state management.

34. What are the similarities and differences between Future and Stream?

35. What is tree shaking in Flutter?

36. Differentiate setState and Provider?

37. Which one is best between React Native and Flutter?

38. Explain Tween Animation?

39. Explain the Stateful Widget Lifecycle?

40. What is a Spacer widget?

41. What is a Navigator and what are Routes in Flutter?

42. What are GlobalKeys?

43.What is an AspectRatio widget used for?


1. What is Flutter?

Flutter is a UI-based tool for creating mobile applications. You can use Flutter to create natively compiled mobile applications with a single programming language and a single codebase. Fast and beautiful mobile applications can be developed with the right skill sets to code in Flutter. Google developed the framework, and the code is open source. Flutter in itself is not a language, and it makes use of Dart language for coding.

Flutter can be used to code for both IOS and Android. The optimization is best suited for 2D mobile apps. The following features can be used to make the apps:-

  • Geolocation
  • Storage access
  • Camera access
  • Network
  • Third-Party SDKs

2. What are the advantages of using Flutter?

Flutter has various advantages to coding mobile apps:-

  1. Reduce the amount of code- Hot reload feature provided by Flutter helps in faster performance. The app is coded in C/C++ code to make it as close to machine code as possible for faster app development. The widget availability in Flutter reduces the time spent on coding and uses reusable code.
  2. Cross-Platform development- Flutter code can be used across platforms to reduce the effort on the development team's side.
  3. Live and Hot Reloading- It helps write the code faster and more easily. The code can be easily modified when a change is made.
  4. Works like a Native app- Flutter code is as close to machine code as possible. This reduces the errors due to the interpretation of code. The app works in a native environment, and the coded apps are fast and easy to use.
  5. Community- Flutter has a thriving community to help you with the questions you might have.
  6. Minimal Code- The Flutter app is coded using the Dart programming language. This increases the speed of development, and the UI is fast. Dart uses JIT and AOT compilation which is very fast.
  7. Faster documentation- It has fast and well-organized documentation. The central depository stores the documents for future reference.
  8. Customized designs- The customized layered architecture allows the designing of custom designs, faster rendering and expressive UIs.

3. What is the Flutter architecture?

Flutter has a three-layered architecture:-

  1. Upper Layer: The upper layer consists of the Dart programming language along with the widgets, animations, illustrations, customizations, etc.
  2. The middle layer or the Flutter Engine: This layer deals with text display, formatting, layout, etc.
  3. Bottom Layer or the built-in service: This layer is for managing plugins or packages.

4. What build modes are available in Flutter?

Flutter is made up of three build modes: -

  1. Debug Mode- This mode is for debugging apps on a device or a simulator.
  2. Profile Mode- Some debugging abilities are available, along with an analysis of app's performance in testing rounds.
  3. Release Mode- This mode is used when deploying the app. This mode is for faster performance. You cannot debug or edit the code in this mode.

5. What is the Dart programming language?

Dart is an object-oriented programming language with a syntax like the C Language. The language is open source and was developed by Google in 2011. The language was conceptualized to code the mobile app frontend. The app is used for coding frontend user interfaces. Flutter apps make use of the Dart programming language.

6. Is Dart language necessary for Flutter?

It is compulsory to know the Dart language to use Flutter. Flutter applications make use of the Dart language for coding mobile phone apps.

7. What are widgets in Flutter?

Flutter apps make use of widgets to code mobile phone applications. You will almost exclusively build on Flutter using widgets. Widgets define the view of the app. When you change the code, the widget code is automatically adjusted. Widgets are nested with each other to get the final design of the app. This means the widget is the base on which the app is coded.

8. What are the most useful editors for Flutter apps?

Flutter tools make use of plugins to code mobile phone apps. The plugins can help in Dart code compilation, analysis of the code, and development of apps. Some used Flutter tools for IDE development are

  • Visual Studio
  • Android Studio
  • Xcode
  • IntelliJ IDEA
  • Eclipse
  • Emacs

9. What are packages and plugins in Flutter?

Similar types of classes, interfaces, and sub-packages are known as packages in Flutter or any other object-oriented language. The packages and plugins are used in development to reduce the coder's effort. The coder need not write the code for everything and can use packages and plugins to reduce the coding effort.

The distinction between packages and plugins is minute. Packages are new components or written code in Dart language. Plugins, on the other hand, use native code to allow more functionality. Sometimes the two terms are confused as the same, but a minute distinction exists.

10. Are there any popular apps which make use of Flutter?

There are many popular apps which use Flutter. Some of the apps are:-

  • Reflectly
  • Google Ads
  • Alibaba
  • Tencent
  • Birch Finance

And many more. The use of Flutter in mobile applications is very high.

11. In What technology is Flutter built?

Flutter is built using C, C++, Skia - 2D rendering engine, and Dart-object-oriented language. 21. Difference between runApp() and main() in flutter.

main()

It is a function used to start the program. 
In Flutter, it is impossible to write any program without the main() function.  

runApp()

It 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. 
This function is called the main function and is also the driver of the app. 

12.What is the App state?

App State is a shared state or application state. App states can be shared across sections of your app and maintain user sessions in the same way. App state includes activities such as login info, user preferences, E-commerce shopping cart, social networking notifications, etc.

13. What do you mean by keys in flutter? and its uses

  • In Flutter, We use Keys as the identifier for Elements, SemanticNodes, and Widgets. We will utilize it when any new widget tries to update the available element; later, its key must be similar to the current widget key related to the element.
  • Keys must not be distinct amongst elements in the same parent.
  • The subclasses of the Key should be a LocalKey or GlobalKey.
  • Keys are helpful when we manipulate(like reordering, adding, or removing) a group of widgets of a similar type that hold a similar state.


Uses:

  • Keys in flutter are used primarily to preserve the state of modified widgets in the widget trees.
  • It is used to reorganize and modify the collections of widgets having equivalent types and defined states.
  • It does not modify a tree that consists of only stateless widgets.

14. What is Flutter Inspector?

The Flutter Inspector is a powerful tool used to visualize the blueprint of your widgets and their properties in Flutter.

  • Flutter Inspector can select widget mode in a widget tree.
  • It provides the toggle platform.
  • It shows paint baselines and debugs paint.
  • It can refresh the widgets as well as show or hide the performance overlay.

15. Different types of streams in Dart

There are two types of streams in Dart, single subscription streams and broadcast streams.

Single Subscription Streams

  • The events within a larger whole are delivered sequentially with single subscription streams.
  • They are used for events that are in received matters or for reading a file.
  • There is only one listener throughout the sequence to trigger the event, else the event won’t be triggered.

Broadcast Streams

  • Initially, the event needs to be subscribed to by the listener, then only these streams deliver events to their subscribers and subscribers can immediately start listening to events.
  • There are several listeners to events simultaneously. Besides, one can also listen to the event again even after canceling a previous subscription.

16. What is the difference between SizedBox and Container?

SizedBox

  • The SizedBox widget in Flutter has a specified size.
  • In SizedBox, we cannot set the color or decoration for the widget.
  • We can only use it with a child widget having a specific width or height.

Container

  • The Container in Flutter is a parent widget containing multiple child widgets to manage them efficiently in width, height, padding, background color, etc.
  • If we have a widget that needs some background styling may be a color, shape, or size constraint that can be wrapped in a container widget.

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

The fat arrow syntax is simply a short hand for returning an expression and is similar to `(){ return expression; }.`

The fat arrow is for returning a single line, braces are for returning a code block.

Only an expression—not a statement—can appear between the arrow (=>) and the semicolon (;). For example, you can’t put an if statement there, but you can use a conditional expression

// Normal function
void function1(int a) {
  if (a == 3) {
    print('arg was 3');
  } else {
    print('arg was not 3');
  }
}

// Arrow Function
void function2(int a) => print('arg was ${a == 3 ? '' : 'not '}3');

18. How many types of widgets are there in Flutter?

There are two types of widgets:

  1. StatelessWidget: A stateless widget can not change their state during the runtime of an app which means it can not redraw its self while the app is running. Stateless widgets are immutable.
  2. StatefulWidget: A stateful widget can redraw itself multiple times, while the app is running which means its state is mutable. For example, when a button is pressed, the state of the widget is changed.

19. When should you use WidgetsBindingObserver?

WidgetsBindingObserver should be used when we want to listen to the AppLifecycleState and call stop/start on our services.

20. What is Dart and why does Flutter use it?

Dart is an object-oriented, garbage-collected programming language that you use to develop Flutter apps. It was also created by Google, but is open-source, and has community inside and outside Google. Dart was chosen as the language of Flutter for the following reason:

  • Dart is AOT (Ahead Of Time) compiled to fast, predictable, native code, which allows almost all of Flutter to be written in Dart. This not only makes Flutter fast, virtually everything (including all the widgets) can be customized.
  • Dart can also be JIT (Just In Time) compiled for exceptionally fast development cycles and game-changing workflow (including Flutter’s popular sub-second stateful hot reload).
  • Dart allows Flutter to avoid the need for a separate declarative layout language like JSX or XML, or separate visual interface builders, because Dart’s declarative, programmatic layout is easy to read and visualize. And with all the layout in one language and in one place, it is easy for Flutter to provide advanced tooling that makes layout a snap.

21. How to define a generic extension in Dart?

This could be done by adding a template parameter, like the below,

extension MyFancyList<T> on List<T> {

int get doubleLength => length * 2;

List<T> operator -() => reversed.toList();

List<List<T>> split(int at) => <List<T>>[sublist(0, at), sublist(at)];

}

22. How do you create a factory?

Factory constructors are developed by using the factory keyword. They can return subtypes or even null.

e.g., factory User.fromXml(String xml).

23. List the responsibilities of FlutterActivity?

FlutterActivity is an Android app that shows a Flutter experience. The different roles of a FlutterActivity are as follows:

  • The Flutter flash screen and the Android launch screen are shown.
  • It alters the appearance of the status bar.
  • It chooses Flutter’s first direction.
  • It chooses an entry point and the execution path for the Dart app bundle.
  • Transparency may be added to the Activity if needed.

24. What is Build context?

In Flutter, the BuildContext is a component of the widgets in the Element tree, with each widget having its BuildContext. It’s primarily used to get a connection to another widget or theme. If we want to use a material design feature, we must relate it to the scaffold, for example. Scaffold.of(context) method may be used to obtain it.

25. How will you declare async function as a variable in Dart?

Async functions are normal functions having variable type just specifies that it returns a Future:

class Example {

Future Function() asyncFuncVar;

Future asyncFunc() async => print(‘Do async stuff…’);

Example() {

asyncFuncVar = asyncFunc;

asyncFuncVar().then((_) => print(‘Hello’));

}

}

void main() => Example();

26. Why do we need Mixins?

Multiple inheritances are not supported in Dart. Mixins are important to formulate multiple inheritances in Flutter/Dart. Mixins allow you to write the code for a reusable class in multiple class hierarchies.

27. Explain Ticker in Flutter?

In Flutter, a ticker represents the rate at which our animation is refreshed. It’s a signal-sending class that sends a signal at a set frequency, such as 60 times per second. Our watch, which tics at regular intervals, helps us grasp it. Ticker offers a callback method for each tick that has the length since the first ticks at each second since it was started. Even if the tickers began at different times, they are immediately synchronized.

28. Explain Stream in Flutter?

A stream is a set of asynchronous events that happen at the same time. It generates an asynchronous data series. It’s like putting any value on one end of a pipe, and if there’s a listener on the other end, it will receive that value. We can keep several listeners in a stream, and when they’re all placed into the pipeline, they’ll all get the same benefit.

The Stream API’s await for and listen() methods can be used to process a stream. It has a mechanism for dealing with errors. Streams can be created in a variety of ways, but they can all be used in the same way. Consider the following condition:

Future<int> sumStream(Stream<int> stream) async {

var sum = 0;

await for (var value in stream) {

sum = sum + value;

}

return sum;

}

29. Explain Null aware pointers?

Dart has some helpful tips for dealing with null values.

  1. The “??=” assignment operator, which only assigns a value to a variable if it is null.
int x; // Initial value of a is null.

x??= 2;

print(x); // It will print 2.
  1. The null-aware “??” operator evaluates and returns the value between two expressions. It checks expression 1 and returns its value if it is not null; otherwise, it evaluates and returns the value of expression 2:
print(4 ?? 5); // It will print 4.

print(null ?? 5); // It will print 5. 

30. How can you run code in debug mode only?

To run the code in debug mode only, we must first import the dart foundation as shown below:

import ‘package:flutter/foundation.dart’ as Foundation;

Next, use the kReleaseMode as shown below:

if (Foundation.kReleaseMode){ // is Release Mode ??

print(‘release mode’);

} else {

print(‘debug mode’);

} 

31. What is Provider?

A Provider allows to not only expose a value, but also create/listen/dispose it. When you place a Provider widget in your widget tree all the Childs of the Provider will have access to the values exposed by it.

32. What are the limitations of Flutter?

  • Lack of Third-Party Libraries and Widgets: Flutter is not too old unlike its contemporaries and lacks the presence of third-party libraries.
  • Issues with iOS: Flutter is developed by Google. This is why developers are worried about its implementation for iOS.
  • Large File Sizes: One big loophole that ois hard to ignore is the large file size of apps developed in Flutter. In some cases these file sizes could be a significant issue.

33. Explain state management.

Managing state in an application is one of the most important and necessary process in the life cycle of an application.

State management can be divided into two categories based on the duration the particular state lasts in an application:

  1. Ephemeral − Last for a few seconds like the current state of an animation or a single page like current rating of a product. Flutter supports this through StatefulWidget.
  2. app state − Lasts for entire application like logged in user details, cart information, etc., Flutter supports this through scoped_model.

34. What are the similarities and differences between Future and Stream?

Similarities

  1. Stream and Future both work asynchronously.
  2. Both have the same potential

Differences

  1. A stream may be a mixture of Futures.
  2. Future has only one response yet Stream can have any number of Responses.

35. What is tree shaking in Flutter?

Tree Shaking is the optimization technique for removing the unutilized module in the bundle in the build process. It is also the code elimination technique used for optimizing our application.

(for flutter web) When compiling a Flutter web application, the JavaScript bundle is generated by the dart2js compiler. A release build has the highest level of optimization, which includes tree shaking your code. Tree shaking is the process of eliminating dead code, by only including code that is guaranteed to be executed. This means that you do not need to worry about the size of your app’s included libraries because unused classes or functions are excluded from the compiled JavaScript bundle

36. Differentiate setState and Provider?

setState()

We use it for managing the local state in a similar stateful widget and its child. The downside is everything is in the same class like UI code, business logic, and mixin UI, which splits clean code principles.

Provider

In the provider pattern, we define everything in a separate class indicating that the UI presentation is defined in the different logics that specify in different thus code appears high quality and clean. Moreover, we don’t need to transmit the state from one screen to another through the constructor.

37. Which one is best between React Native and Flutter?

Flutter and React Native both are utilized for developing the native hybrid app through a single codebase. These applications can be executed on Android and iOS platforms.

  1. React Native was developed by Facebook, while Flutter Framework was developed by Google. Therefore, both frameworks have good features and communities.
  2. Flutter utilizes Dart programming language for creating the applications and React Native utilizes Javascript for developing the applications.
  3. From the Developer’s perspective, it is very complex to select them. Therefore, it is very hard to select the winner between React Native and Flutter.

38. Explain Tween Animation?

Tween Animation is a short form of in-betweening. In Tween Animation, we must define the starting and endpoint of the automation. It indicates that the animation starts with the starting value, after that, it goes through a series of intermediary values, and lastly arrives at the end value. It also offers the curve and timeline that defines the speed and time of the variation.

39. Explain the Stateful Widget Lifecycle?

The lifecycle has the following simplified steps:

40. What is a Spacer widget?

Spacer manages the empty space between the widgets with flex container. Evenly with the Row and Column MainAxis alignment we can manage the space as well

41. What is a Navigator and what are Routes in Flutter?

Navigation and routing are some of the core concepts of all mobile application, which allows the user to move between different pages. We know that every mobile application contains several screens for displaying different types of information. For example, an app can have a screen that contains various products. When the user taps on that product, immediately it will display detailed information about that product

42. What are GlobalKeys?

GlobalKeys have two uses: they allow widgets to change parents anywhere in your app without losing state, or they can be used to access information about another widget in a completely different part of the widget tree. An example of the first scenario might if you wanted to show the same widget on two different screens, but holding all the same state, you’d want to use a GlobalKey

43.What is an AspectRatio widget used for?

AspectRatio Widget tries to find the best size to maintain aspect ration while respecting it’s layout constraints. The AspectRatio Widget can be used to adjust the aspect ratio of widgets in your app