Consider this Flutter widget that uses GetIt to get a service instance. What will be displayed on the screen?
import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; class CounterService { int count = 0; void increment() => count++; } final getIt = GetIt.instance; void setup() { getIt.registerSingleton<CounterService>(CounterService()); } class CounterWidget extends StatelessWidget { @override Widget build(BuildContext context) { final counter = getIt<CounterService>(); counter.increment(); return Text('Count: ${counter.count}'); } } void main() { setup(); runApp(MaterialApp(home: Scaffold(body: Center(child: CounterWidget())))); }
Think about when the increment() method is called and how many times the widget builds.
The CounterService is registered once as a singleton. When CounterWidget builds, it calls increment() once, so the count becomes 1. The widget then displays 'Count: 1'.
Given this Flutter app using GetIt singleton service, what will be the value of count after the button is pressed 3 times?
import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; class CounterService { int count = 0; void increment() => count++; } final getIt = GetIt.instance; void setup() { getIt.registerSingleton<CounterService>(CounterService()); } class CounterApp extends StatefulWidget { @override State<CounterApp> createState() => _CounterAppState(); } class _CounterAppState extends State<CounterApp> { final counter = getIt<CounterService>(); @override Widget build(BuildContext context) { return Scaffold( body: Center(child: Text('Count: ${counter.count}')), floatingActionButton: FloatingActionButton( onPressed: () { setState(() { counter.increment(); }); }, child: Icon(Icons.add), ), ); } } void main() { setup(); runApp(MaterialApp(home: CounterApp())); }
Each button press calls increment() once and triggers a rebuild.
The CounterService is a singleton, so the same instance is used. Each button press increments count by 1. After 3 presses, count is 3.
Which code snippet correctly registers a lazy singleton service with GetIt so that the instance is created only when first used?
Lazy singleton means the instance is created only when requested first time.
Option D uses registerLazySingleton with a factory function, which creates the instance lazily. Option D registers an immediate singleton, A registers a factory (new instance every time), and D is invalid because it passes an instance instead of a factory.
Given this code snippet, why does it throw a GetItNotFoundException when trying to get the service?
final getIt = GetIt.instance; class ApiService {} void main() { // Missing registration final api = getIt<ApiService>(); }
Check if the service was registered before calling getIt.
GetIt throws GetItNotFoundException if you try to get a service that was never registered. Here, ApiService was not registered before retrieval.
Choose the best explanation for why developers use GetIt for dependency injection in Flutter apps.
Think about how dependency injection helps with code structure and testing.
GetIt helps separate how objects are created from where they are used. This decoupling makes it easier to swap implementations, write tests, and maintain code. It does not generate UI code, replace widget trees, or enforce compile-time checks beyond Dart's normal typing.