Sobes.tech
Junior — Middle

Have you worked with dependency injection mechanisms in projects?

sobes.tech AI

Answer from AI

Dependency Injection (DI) in Flutter helps manage dependencies between objects, making testing and maintaining code easier. Commonly, packages like provider, get_it, or riverpod are used.

Example using get_it:

import 'package:get_it/get_it.dart';

final getIt = GetIt.instance;

class ApiService {
  void fetchData() {
    print('Fetching data...');
  }
}

void setup() {
  getIt.registerLazySingleton<ApiService>(() => ApiService());
}

void main() {
  setup();
  var api = getIt<ApiService>();
  api.fetchData();
}

This approach allows centralized management of dependencies and easy replacement of implementations, for example, for testing.