0
0
Fluttermobile~20 mins

Dependency injection (GetIt) in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GetIt Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flutter widget using GetIt?

Consider this Flutter widget that uses GetIt to get a service instance. What will be displayed on the screen?

Flutter
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()))));
}
ACount: 1
BRuntime error: Service not found
CCount: 2
DCount: 0
Attempts:
2 left
💡 Hint

Think about when the increment() method is called and how many times the widget builds.

state_output
intermediate
2:00remaining
What is the value of count after multiple widget rebuilds?

Given this Flutter app using GetIt singleton service, what will be the value of count after the button is pressed 3 times?

Flutter
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()));
}
ACount: 0
BCount: 3
CCount: 1
DCount: 6
Attempts:
2 left
💡 Hint

Each button press calls increment() once and triggers a rebuild.

📝 Syntax
advanced
2:00remaining
Which option correctly registers a lazy singleton with GetIt?

Which code snippet correctly registers a lazy singleton service with GetIt so that the instance is created only when first used?

AgetIt.registerFactory(() => MyService());
BgetIt.registerSingleton(() => MyService());
CgetIt.registerLazySingleton(MyService());
DgetIt.registerLazySingleton(() => MyService());
Attempts:
2 left
💡 Hint

Lazy singleton means the instance is created only when requested first time.

🔧 Debug
advanced
2:00remaining
Why does this GetIt code throw an error at runtime?

Given this code snippet, why does it throw a GetItNotFoundException when trying to get the service?

Flutter
final getIt = GetIt.instance;

class ApiService {}

void main() {
  // Missing registration
  final api = getIt<ApiService>();
}
AThe ApiService was never registered with GetIt before retrieval.
BThe ApiService class is abstract and cannot be instantiated.
CGetIt requires async initialization before use.
DThe GetIt instance was not created with GetIt.instance.
Attempts:
2 left
💡 Hint

Check if the service was registered before calling getIt().

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using GetIt for dependency injection in Flutter?

Choose the best explanation for why developers use GetIt for dependency injection in Flutter apps.

AIt replaces Flutter's widget tree with a service-oriented architecture.
BIt automatically generates UI code based on service definitions.
CIt allows decoupling of object creation from usage, making testing and maintenance easier.
DIt enforces strict compile-time type checking for all dependencies.
Attempts:
2 left
💡 Hint

Think about how dependency injection helps with code structure and testing.