Menu

GetX in Flutter — Part 2: Dependency Injection and a Verdict
📰

GetX in Flutter — Part 2: Dependency Injection and a Verdict

DEV Community: dart·Yadnesh Teli·7 days ago
#8gRTCxr1
#devtodart#devto#dev#getx#dependency
Reading 0:00
15s threshold

Series 3 — Part 2 of 2

GetX in Flutter — Part 2: Dependency Injection and a Verdict

Series: Exploring Flutter State Management Alternatives

In Part 1 I built a basic GetX app and explored its state management and navigation. In this post I look at dependency injection, fix the bugs I introduced along the way, and land on a verdict for when I would choose GetX over Riverpod.

Dependency injection in GetX

GetX uses a service locator pattern. You register a dependency with Get.put() or Get.lazyPut() , and retrieve it anywhere with Get.find() . This is fundamentally different from Riverpod's compile-time provider graph.

// Register Get . put ( ProductController ());

// Retrieve anywhere final controller = Get . find < ProductController >();

Enter fullscreen mode

Exit fullscreen mode

The advantage is simplicity. The disadvantage is that Get.find() can throw at runtime if the dependency has not been registered yet. With Riverpod, the compiler would catch missing providers.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.