flutter_clean_arch_templ
flutter_clean_arch_templ copied to clipboard
flutter_clean_arch_templ
A Flutter Rick and Morty App in Clean Architecture.
Flutter Interview Questions
-
What is the difference between a StatelessWidget and a StatefulWidget in Flutter?
StatelessWidget: Represents a widget that cannot change its internal state once it's built. It's used for UI elements that don't need to be updated dynamically.StatefulWidget: Represents a widget that can change its internal state and trigger a rebuild when that state changes. It's used for dynamic UI elements that need to respond to user interactions or data changes.
-
Explain the Stateful Widget Lifecycle?
createState: Initializes the mutable state of the widget.build: Rebuilds the widget in response to state changes.didUpdateWidget: Called when the parent widget changes and the state needs to be updated.dispose: Called when the widget is removed from the tree, allowing for cleanup.
-
When do you use the WidgetsBindingObserver?
- You use
WidgetsBindingObserverto listen for application lifecycle events likedidChangeAppLifecycleState. It's useful for handling scenarios when your app goes into the background or returns to the foreground.
- You use
-
What is Flutter tree shaking?
- Tree shaking is a process where Flutter's build tools eliminate unused code and resources from the final app bundle. This helps reduce the app's size by removing unnecessary components.
-
What is a Spacer widget?
Spaceris a widget that takes up available space along the main axis in aRoworColumn. It's used to distribute space evenly among other widgets.
-
What is the difference between hot restart and hot reload?
- Hot Restart: Restarts the entire app and reloads all code, including the app's initial state. It's slower but helps fix some errors.
- Hot Reload: Reloads only the updated code and state without restarting the app. It's faster and used during development to see changes quickly.
-
What is an InheritedWidget? List some examples.
InheritedWidgetis a way to share data down the widget tree. Examples includeTheme,MediaQuery, and custom data-sharing widgets.
-
Why is the build() method on State and not StatefulWidgets?
- The
build()method is on theStateclass because it represents the mutable state of aStatefulWidget. TheStateobject is responsible for rendering the UI based on that state.
- The
-
What is a pubspec file in Dart?
- A
pubspec.yamlfile is used to define the metadata and dependencies for a Dart package, including Flutter apps. It specifies package name, version, dependencies, and other project settings.
- A
-
How is Flutter native?
- Flutter compiles to native machine code for the target platform (e.g., ARM for Android). It uses the platform's native widgets and has direct access to platform-specific features, making it native in performance and look.
-
What is a Navigator and what are Routes in Flutter?
Navigatormanages a stack ofRouteobjects, which represent screens or pages in a Flutter app. It allows you to navigate between different parts of your app.
-
What is a PageRoute?
- A
PageRouteis a route that transitions between pages or screens in a Flutter app. Examples includeMaterialPageRouteandCupertinoPageRoute.
- A
-
Explain async, await and Futures.
asyncandawaitare used for asynchronous programming in Dart and Flutter.asyncmarks a function as asynchronous, andawaitis used to pause the function's execution until aFuturecompletes.Futurerepresents a potential value or error that will be available at some point in the future.
-
How can you update a ListView dynamically?
- You can update a
ListViewdynamically by modifying the underlying data source (e.g., aList) and then callingsetState()to trigger a rebuild of the widget tree.
- You can update a
-
What is a Stream?
- A
Streamis a sequence of asynchronous events or data. It allows you to receive and process data as it becomes available, often used for real-time data handling.
- A
-
What are keys in Flutter and when should you use them?
- Keys are used to uniquely identify widgets in Flutter. They are useful when you need to rebuild a specific widget or manage widget state across rebuilds.
-
What are GlobalKeys?
GlobalKeyis a special type of key that allows you to access and manipulate the state of a widget from anywhere in your app. It's often used with widgets likeFormorScaffoldto interact with their state.
-
When should you use mainAxisAlignment and crossAxisAlignment?
mainAxisAlignmentis used to align children along the main axis (e.g., horizontally in aRow).crossAxisAlignmentis used to align children along the cross axis (e.g., vertically in aRow).
-
When can you use double.INFINITY?
- You can use
double.INFINITYto indicate an unconstrained size for a widget, allowing it to take up as much available space as needed.
- You can use
-
What is Ticker, Tween, and AnimatedBuilder?
Tickeris used to control animations in Flutter.Tweendefines a range of values for animations.AnimatedBuilderis a widget used to build animations based on aTweenand aTicker.
-
What is ephemeral state?
- Ephemeral state refers to state that is local to a single widget and doesn't need to be shared or persisted beyond the widget's lifetime.
-
What is an AspectRatio widget used for?
AspectRatiois used to enforce a specific aspect ratio for its child. It resizes its child while maintaining the specified aspect ratio.
-
How would you access StatefulWidget properties from its State?
- You can access
StatefulWidgetproperties from itsStateusing thewidgetproperty of theStateclass, likewidget.propertyName.
- You can access
-
Is there a suggested limit to the number of FloatingActionButton a screen can have? Give a reason(s) for your answer.
- There is no strict limit, but it's advisable to keep the number of
FloatingActionButtonwidgets reasonable to maintain a clean and user-friendly UI. Overloading a screen with too many buttons can make it confusing and cluttered.
- There is no strict limit, but it's advisable to keep the number of
-
Mention two or more operations that would require you to use or return a Future.
- Network requests (e.g., fetching data from a server).
- File operations (e.g., reading or writing files).
- Database queries.
- Long-running computations.
-
What is the purpose of a SafeArea?
SafeAreais used to ensure that the content within it is displayed within the safe, viewable area of the screen, avoiding areas like notches and system bars.
-
When to use a mainAxisSize?
mainAxisSizeis used inRowandColumnwidgets to control how they allocate space along their main axis.
You use it to determine whether the widget should take up as much space as possible (MainAxisSize.max) or as little as possible (MainAxisSize.min).
-
SizedBox VS Container?
SizedBoxis a simple box with a fixed size. It doesn't have child widgets.Containercan contain child widgets and provides more styling options like padding, margin, and decoration.
-
List the Visibility widgets in flutter and the differences?
- The
Visibilitywidget controls the visibility of its child. - The
Offstagewidget also hides its child but doesn't allocate space for it.
- The
-
Can we use Color and Decoration property simultaneously in the Container? Explain.
- No, you generally can't use both
ColorandDecorationproperties simultaneously in aContainer.Decorationproperty is used for more advanced styling, and it overrides theColorproperty.
- No, you generally can't use both
-
In order for the CrossAxisAlignment.baseline to work, what is another property that we need to set?
- To use
CrossAxisAlignment.baseline, you need to specify thebaselineproperty in theTextBaselineenum for the children you want to align along their baselines.
- To use
-
When should we use a resizeToAvoidBottomInset?
resizeToAvoidBottomInsetshould be used when you want to automatically resize the body of aScaffoldto avoid the on-screen keyboard (soft keyboard) that might cover the text fields or input elements.
-
What is the difference between 'as,' 'show,' and 'hide' in an import statement?
asis used to create an alias for an imported library or symbol.showis used to explicitly import only specific symbols from a library.hideis used to exclude specific symbols from being imported from a library.
-
What is the importance of a TextEditingController?
TextEditingControlleris used to control and manipulate the content of a text input field (e.g.,TextField). It allows you to read and modify the text input's value.
-
Why do we use a Reverse property in a ListView?
- The
reverseproperty in aListViewis used to reverse the order of its children. This can be useful when you want to display a list in reverse chronological order, such as a chat history.
- The
-
Difference between a Modal and Persistent BottomSheet with an example?
- A modal bottom sheet covers the entire screen when displayed and is typically used for actions or selections that need user attention.
- A persistent bottom sheet is a smaller sheet that remains visible at the bottom of the screen and is often used for additional information or controls.
-
How is an Inherited Widget different from a Provider?
InheritedWidgetis a Flutter class for sharing data across the widget tree, whereas "Provider" is a design pattern or library for state management that can useInheritedWidgetunder the hood but provides additional features and flexibility.
-
What is an UnmodifiableListView?
- An
UnmodifiableListViewis a read-only view of a list that prevents modifications like adding or removing elements. It's useful when you want to expose a list without allowing external changes.
- An
-
Difference between these operators "?? and ?."
??is the null coalescing operator and returns the right-hand value if the left-hand value is null.?.is the conditional access operator and is used to access properties or methods of an object if the object is not null; otherwise, it returns null.
-
What is the purpose of ModalRoute.of()?
ModalRoute.of(context)is used to retrieve the current modal route in the widget tree. It allows you to access properties and methods of the current route, such as the route's name or settings.
-
Difference between Navigator.pushNamed and Navigator.pushReplacementNamed?
Navigator.pushNamedis used to push a new route onto the stack and add it to the navigation history.Navigator.pushReplacementNamedis used to replace the current route in the stack with a new route. It removes the current route from the history.
-
Difference between a Single Instance and Scoped Instance?
- In the context of state management, a single instance typically refers to a global singleton object that holds and manages application-wide state.
- A scoped instance, on the other hand, is a state instance scoped to a specific part of the widget tree, often used with libraries like "Provider" to manage localized state within a widget subtree.
Flutter Animation Interview Questions
-
What is a vsync?
vsyncstands for "vertical sync." In the context of animations in Flutter, it's a mechanism used to synchronize animations with the device's screen refresh rate. It helps ensure that animations are smooth and not out of sync with the screen's rendering.
-
When does the animation reach completed or dismissed status?
- In Flutter's animation framework, an animation reaches the "completed" status when it has progressed from its initial state to its final state. Conversely, it reaches the "dismissed" status when it has reversed from the final state back to the initial state.
-
Difference between
AnimationControllerandAnimation?AnimationControlleris a controller that manages an animation's state, duration, and other properties. It's responsible for controlling the animation's progression and can be used to start, stop, or reverse an animation.Animationrepresents the current value of an animation at any given point in time. It doesn't control the animation's state but rather provides access to the animated values.
-
When to use
SingleTickerProviderStateMixinandTickerProviderStateMixin?SingleTickerProviderStateMixin: Use this mixin when you have only oneAnimationControllerin your widget and want to provide it as avsyncto ensure smooth animations.TickerProviderStateMixin: Use this mixin when you have multipleAnimationControllerinstances in your widget and want to provide avsyncfor each of them individually.
-
Define a TweenAnimation?
- A TweenAnimation is an animation that uses a
Tweento interpolate values between a specified range or values. It allows you to animate properties smoothly from one value to another. For example, animating the opacity of a widget from 0.0 to 1.0 using aTween<double>.
- A TweenAnimation is an animation that uses a
-
State the importance of a Ticker?
- A Ticker is a crucial part of Flutter's animation system. It provides a way to synchronize animations with the device's screen refresh rate (vsync), ensuring that animations appear smooth and visually appealing. Tickers help control the timing and progression of animations.
-
Why do we need mixins?
- Mixins are a way to reuse and share code between classes in Dart and Flutter. They allow you to add features or behavior to a class without inheriting from that class, promoting code modularity and reusability.
- In the context of Flutter animations, mixins like
SingleTickerProviderStateMixinandTickerProviderStateMixinprovide specific behavior (e.g.,vsynccapability) to widget classes without the need for multiple inheritance.
Mixins are valuable for creating flexible and maintainable code by allowing you to compose classes with different sets of behaviors while avoiding the constraints of single inheritance.
App Structure
Screenshots
