dart_service_locator

Creator: coderz1093

Last updated:

0 purchases

dart_service_locator Image
dart_service_locator Images

Languages

Categories

Add to Cart

Description:

dart service locator

Flutter Service Locator #
A lightweight and easy-to-use dependency injection package for Flutter applications.
Features #

Simple API for registering and resolving dependencies
Support for both synchronous and asynchronous dependency resolution
Singleton and factory instance management
Built-in disposal mechanism for cleaning up resources
Minimal boilerplate code required

Installation #
Add this to your package's pubspec.yaml file:
dependencies:
flutter_service_locator: any
copied to clipboard
Then run:
$ flutter pub get
copied to clipboard
Usage #
Setting up dependencies #
import 'package:dart_service_locator/dart_service_locator.dart';

void setupDependencies() {
// Register a synchronous dependency
register<Logger>(() => ConsoleLogger());
register<User>(() => User());

// Register an asynchronous dependency
register<Future<Database>>(() async {
final db = Database();
await db.initialize();
return db;
});

// Register a dependency that relies on other dependencies
register<Future<UserRepository>>(() async {
final db = await locate<Future<Database>>();
return UserRepository(db);
});
}
copied to clipboard
Using dependencies #
import 'package:flutter/material.dart';
import 'package:dart_service_locator/dart_service_locator.dart';

class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder<UserRepository>(
future: locate<Future<UserRepository>>(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final userRepo = snapshot.data!;
// Use userRepo
return Text('User count: ${userRepo.getUserCount()}');
} else {
return CircularProgressIndicator();
}
},
);
}
}
copied to clipboard
Cleaning up #
import 'package:dart_service_locator/dart_service_locator.dart';

clear();
copied to clipboard
API Reference #

register<T>(T Function() creator): Register a synchronous dependency
locate<T>(): Get or create a locate instance of a dependency
create<T>(): Create a new instance of a dependency
remove<T>(): Remove a locate instance of a dependency
clear(): Clear all registered dependencies

Examples #
For more advanced usage and examples, check out the example folder in the package repository.
Additional Information #
For more information on using this package, please refer to the API documentation.
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the BSD-3-Clause license - see the LICENSE file for details.

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.