Fang 🦷

Fang is a lightweight, zero-boilerplate service locator for Dart. It was originally designed to solve the issue of accessing non-const dependencies within const callbacks (like autocomplete handlers in nyxx), but it's useful anywhere you need a simple way to manage global state or dependencies.

No magic. No framework. Just teeth.


Features

  • Extremely simple API: register / get (or bite / hunt if you're feeling wild!)
  • Lazy Loading: Initialize services only when they are needed.
  • Zero Configuration: No complex setup or code generation.
  • Lightweight: Minimal overhead and no external dependencies.
  • **Vegan **: No animals were harmed in the making of this service locator.

Getting Started

Add Fang to your project:

dart pub add fang

Usage

Basic Registration and Retrieval

Register an instance once, and retrieve it from anywhere else in your code. You can use the standard names or the thematic aliases.

import 'package:fang/fang.dart';

final class CustomClient {
  String doWork() => 'Work done!';
}

void main() {
  // Register the instance once
  Fang.register(CustomClient());
  // Or: Fang.bite(CustomClient());

  // Retrieve it later
  final client = Fang.get<CustomClient>();
  // Or: final client = Fang.hunt<CustomClient>();
  
  print(client.doWork());
}

Lazy Registration

Use registerLazy if you want to delay the creation of your service until it's actually requested.

Fang.registerLazy(() => DatabaseClient());

// The factory function is called here for the first time
final db = Fang.getLazy<DatabaseClient>();

Lifecycle Management

Fang provides simple methods to manage your registered instances:

// Check if a type is registered
if (Fang.exists<MyService>()) {
  print('Service is ready');
}

// Remove a specific instance
Fang.remove<MyService>();

// Clear all registered instances (useful for testing)
Fang.clear();

Libraries

fang