Dart, widgets, state management, rendering
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeNull safety in Dart ensures that variables can't be null unless explicitly declared as nullable. This helps catch null reference errors at compile time, making the code safer and reducing runtime crashes.
I declare an asynchronous function using the async keyword, and I can use await inside it to wait for Futures to complete. For example: Future<void> fetchData() async { await someAsyncCall(); }.
A StatelessWidget doesn't store any mutable state and is immutable, so it only rebuilds when its parent changes. A StatefulWidget can hold state that changes over time, and it rebuilds when that state updates.
The build method returns a widget tree that describes how the UI should look for that widget. It's called whenever the widget needs to be rendered or updated.
The widget tree defines the UI structure, the element tree manages widget instances and their state, and the render tree handles layout and painting. These layers help Flutter efficiently update and render the UI.
For a medium-sized app, I often use Provider or Riverpod for state management because they're simple yet scalable. They allow me to share state across widgets and rebuild only the parts of the UI that need updating.
I use platform channels, which let me send messages between Dart and native code. On the Dart side, I use a MethodChannel, and on the native side, I implement the corresponding handler to execute platform-specific functionality.
Jank happens when the UI can't keep up with the frame rate, often due to heavy computation or expensive widget builds on the main isolate. I avoid it by keeping build methods lightweight and moving heavy work to isolates or using compute().
Riverpod offers better testability, improved compile-time safety, and doesn't depend on the widget tree, making it more flexible for complex apps. It also avoids some limitations of Provider, like context scoping issues.
Bloc uses streams and events to manage state. You dispatch events to the Bloc, which processes them and emits new states, making state transitions predictable and easy to test.
I use a StatefulWidget when the UI needs to update in response to user interaction or data changes, like toggling a switch or updating a counter. StatelessWidget is better for static content.
I'd start by profiling the app with Flutter DevTools to identify dropped frames and slow widget builds. Then, I'd look for expensive operations in the build or layout phases, optimize them, and move heavy computations to isolates if needed.